Home  >  Article  >  Backend Development  >  PHP OOP 私有属性赋值纳闷

PHP OOP 私有属性赋值纳闷

WBOY
WBOYOriginal
2016-06-13 12:01:401201browse

PHP OOP 私有属性赋值困惑
最近发现在class 中给私有变量赋值的时候调用了函数就会报错,不得其解释,OOP方面的菜鸟,请高手解释?

<br />class Test extends Controller {<br />    //测试<br />   private $test = count('csdn');<br />}<br />

Parse error: syntax error, unexpected '(', expecting ',' or ';'
------解决方案--------------------
这并非是私有属性的原因
而是类属性在声明时不能赋予不确定的值!

------解决方案--------------------
define('TEST', strlen('csdn'));<br />class Test extends Controller {<br />    //测试<br />   private $test = TEST;<br />}
这样写是可以的

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn