PHP - $total = 0 and var $total = 0. Which is the correct one?
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
What does PHP keyword 'var' do?
我知道这听起来像一个奇怪的问题,但这是我无法摆脱的问题。 我正在定义一个类来处理发票行和它们各自的税收,我现在的'原型'看起来像这样:
1 2 3 4 5 6 7 8 9 10 11 | public function getRate() { if (!empty($this->lines)) { var $total = 0; foreach ($this->lines as $line) { $total += $line->subtotal; } return ['title' => $this->attributes['title'], 'value' => $this->attributes['value'], 'type' => $this->attributes['type'], 'total' => $total]; } } |
$ total只是计算行的总数(在这种情况下是每个税率),所以在我的想法中,一旦函数返回数组就必须丢弃它。 问题是......哪个更合适(更正确)?
Note: The PHP 4 method of declaring a variable with the var keyword is still supported for compatibility reasons (as a synonym for the public keyword). In PHP 5 before 5.1.3, its usage would generate an E_STRICT warning.
在任何情况下,变量都在函数内部,而不在类内部,因此可见性无论如何都不适用。 只需删除