我上课了
Class User { private $_name; private $_email; public static function factory() { return new __CLASS__; } public function test() { } }
User::factory();
它会引起我以下语法错误.
Parse error: Syntax error,unexpected T_CLASS_C in htdocs/test/index.PHP on line 8
因为Static factory()方法在静态方法调用期间无法创建对象,所以会抛出错误.
当我将魔术常数__CLASSS__更改为当前类的名称,即用户,那么它可以工作.
我失踪了什么
尝试:
Class User { private $_name; private $_email; public static function factory() { $class = __CLASS__; return new $class; } public function test() { } }