Xem mẫu

Chapter 2 When dealing with English characters, UTF-8 uses the same encodings as ASCII and ANSII. This has a purposeful consequence; UTF-8 encoded strings that use these characters appear identical to their ASCII and ANSII alternatives. Applications that are Unicode unaware are therefore able to handle many UTF-8 strings. One such application that is not Unicode aware is PHP. We therefore have to be careful when manipulating strings. PHP assumes all characters are eight bits (one byte), but because UTF-8 encoded characters can be longer, this can cause corruption of Unicode data. There is a PHP module, mbstring, which adds support for multi-byte character encodings; unfortunately, not all PHP systems have the mbstring module. In Joomla! we are provided with the static JString class; this class allows us to perform many of the normal string manipulation functions with UTF-8 characters. This example demonstrates how we can use JString to convert a string to upper case. Note that the method name is identical to the PHP function we would normally use: $string = JString::strtoupper($string); The following table describes the PHP string functions and the corresponding JString methods: PHP Function strpos substr strtolower strtoupper strlen str_ireplace str_split strcasecmp strcspn stristr strrev strspn substr_replace ltrim JString method strpos substr strtolower strtoupper strlen str_ireplace str_split strcasecmp strcspn stristr strrev strspn substr_replace ltrim Description Finds the first occurrence of a string in a string. Gets a portion of a string. Converts a string to lowercase. Converts a string to uppercase. Counts the length of a string. Substitutes occurrences of a string with another string in a string (case insensitive). Splits a string into an array. Compares strings. Gets the length of the string before characters from the other parameters are found. Finds the first occurrence of a string in a string (case insensitive). Reverses a string. Counts the longest segment of a string containing specified characters. Replaces a defined portion of a string. Removes white space from the left of a string. [ 35 ] Getting Started PHP Function rtrim trim ucfirst ucwords JString method rtrim trim ucfirst ucwords transcode Description Removes white space from the right of a string. Removes white space from both ends of a string. Converts the first character to uppercase. Converts the first character of each word to uppercase. Converts a string from one encoding to another. Requires the PHP iconv module. Coding Standards Using a standardized format makes code easier to read and allows other developers to edit code more easily. Joomla! uses the PEAR coding standards. A complete guide to the PEAR coding standards is available at http://pear.php.net/manual/en/ standards.php. Here is a break down of the more common rules: • Indents are four spaces: { //fourspacebeforeme! • Control structures have one space between the name and first parenthesis: if(true){ • Use curly braces even when they are optional. • Functions and methods are named using the camelCase standard with a lowercase first character. • Functions and method declarations have no spaces between the name and first parenthesis. Parameter lists have no spaces at the ends. Parameters are separated by one space: foo($bar0,$bar1,$bar2); • Optional function and method parameters must be at the end of the parameter list. Optional parameter values, signified by an equals sign, are separated by spaces: functionfoo($bar0,$bar1,$bar2=``) • Use phpDocumentor tags to comment code http://www.phpdoc.org/. • Use include_once() and require_once() in preference to include() and require(). • Use in preference to all other PHP code block delimiters. [ 36 ] Chapter 2 phpDocumentor phpDocumentor is a documentation tool that allows us to easily create documentation from PHP source code. The documentation is extracted from the source and from special comments within the source; these comments are very similar to those used by JavaDoc. This example demonstrates how we might document a simple function: /** * Adds two integers together * * @param int $value1 Base value * @param int $value2 Value to add * @return int Resultant vaue */ function addition($value1, $value2) { return ((int)$value1 + (int)$value2) } The multiline comment denotes a DocBlock, notice that it uses a double asterisk at the start. The first line is a general description of the function, this description can span more than one line. @param and @return are tags. The @param tag is used to define a parameter in the format (the name is optional): @param type [$name] description The @return tag is used to define the return value in the format: @return type description So our initial example is telling us that the addition() function has two integer parameters named that it will add togther and return the resultant integer value. When we document complex functions, we might want to provide two descriptions, a long description and a short description. This example demonstrates how we do this: /** * Does some complex processing * * A verbose description of the function that spans more than * one line * * @param int $value1 Base value [ 37 ] Getting Started * @param int $value2 Value to add * @return int Resultant vaue */ function someComplexFunction($value1, $value2) { // does some complex processing } Functions are not the only elements that can be documented. Elements that we can document include: • class methods • class varaibles • classes • define() • files • function declarations • global variables (requires use of the @global tag) • include()/include_once() • require()/require_once() This list defines some common tags we are likely to encounter: • @access private|protected|public • @author name • @param type [$name] description • @return type description • @static The DocBlocks are easy to read when they are displayed in code, but, more importantly, we can automatically create documentation from the source code. For more information about using phpDocumentor please refer to http://www.phpdoc.org/. [ 38 ] Chapter 2 Summary The application embodies the complete process of responding to a request. The document is used to determine the format of the response data and as a buffer to store the response data. Instead of using the request and session hashes in Joomla!, we use the static JRequest class and the global JSession object. The JRoute class enables us to parse and build internal URIs. The JText class is used to translate strings into different languages. Limitations in PHP means we must use JString to handle UTF-8 data; if we do not we run the risk of corrupting data. Although the coding standards that we use are ultimately up to us, we should consider using the same standards as those implemented by the Joomla! project. If we chose not to use these standards, we should still consider adding doctags to our classes and functions because they can greatly decrease development and debug time. [ 39 ] ... - tailieumienphi.vn
nguon tai.lieu . vn