Home > Backend Development > PHP Tutorial > PHP OOP 私有属性赋值困惑

PHP OOP 私有属性赋值困惑

WBOY
Release: 2016-06-23 13:54:11
Original
1049 people have browsed it

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

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

Parse error: syntax error, unexpected '(', expecting ',' or ';'


回复讨论(解决方案)

测试写错了,是字符串长度

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


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

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

Parse error: syntax error, unexpected '(', expecting ',' or ';'

这并非是私有属性的原因
而是类属性在声明时不能赋予不确定的值!

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

多谢,多谢

这并非是私有属性的原因
而是类属性在声明时不能赋予不确定的值!

受教了

define('TEST', strlen('csdn'));class Test extends Controller {    //测试   private $test = TEST;}
Copy after login
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