4.函数和方法
函数(function)只能用以字母开头的字母数字。
zf建议所有的function都放到class中。
函数名必须是小写字母开头,如果是多个单词组成,其余单词必须是大写字母。(camelCaps)
例如:getDate(),getElementById()
在面向对象中,成员变量的访问需要使用set/get,这个设计模式有提到。
例如:private $_myz; public function setMyz(){}; public function getMyz(){};
5.函数和方法
5.1.函数和方法的声明
类中的函数必须使用private,protected,public。
和类的格式相同,也需要大扩号单独占一行。
函数后面的()紧贴函数名。
/**
* Documentation Block Here
*/
class Foo
{
/**
* Documentation Block Here
*/
public function bar()
{
// entire content of function
// must be indented four spaces
}
}
函数后面的参数也可以使用引用变量public function bar(&$bar),这个必须先定义成&才能用。
return($this->bar);//这个是错误的。要写成:return $this->bar; 提高可读性。
7.2.文件注释
每个文件在开始都要有注释,符合phpdocumentor标准的。
/**
* Short description for file
*
* Long description for file (if any)...
*
* LICENSE: Some license information
*
* @copyright2005 Zend Technologies
* @license http://www.zend.com/license/3_0.txt PHP License 3.0
* @version CVS: $Id:$
* @link http://dev.zend.com/package/PackageName
* @since File available since Release 1.2.0
*/
7.3.类注释
/**
* Short description for class
*
* Long description for class (if any)...
*
* @copyright2005 Zend Technologies
* @license http://www.zend.com/license/3_0.txt PHP License 3.0
* @version Release: @package_version@
* @link http://dev.zend.com/package/PackageName
* @since Class available since Release 1.2.0
* @deprecated Class deprecated in Release 2.0.0
*/