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

PHP OOP 私有属性赋值纳闷

WBOY
Release: 2016-06-13 12:01:40
Original
1213 people have browsed it

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

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

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

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template