【发布时间】:2014-10-15 00:10:11
【问题描述】:
我很困惑是使用静态方法还是简单方法。
让我举个例子,我正在使用 Zend 框架 1 项目。
我有课像
class Example1
{
public static function getVariable() {
return is_numeric(Zend_Registry::get('config')->Variable) ? Zend_Registry::get('config')->Variable : 0;
}
public function calculateSome($param1, $param2) {
$response = array();
if($param2 == 0) {
$response = number_format(($param1 * self::getvariable()) /100);
} else {
$response = $param1;
}
return $response;
}
}
用法:
- 目前,我在整个项目中获得了像
Example1::getVariable()这样的变量值。 - 和计算就像首先实例化一个类
$class1 = new Example1();,然后像$class1->calculateSome(1, 0);一样调用函数
我很困惑将calculateSome() 更改为public static 并像这样调用Example1::calculateSome(1, 0) 还是保持原样。
我找到了何时使用静态链接 => When to use static vs instantiated classes
但我不明白它在说什么。
【问题讨论】:
标签: php zend-framework static instantiation