PHP的工作原理如何:
$myClass = 'App\MyClass'; $object = new $myClass;
但这会导致错误:
$myClass = 'MyClass'; $object = new 'App\\'.$myClass;
在第二个示例中,抛出了一个意外的T_CONSTANT_ENCAPSED_STRING.
事实证明,上述例子是因为operator precedence,因为新的最高优先级,但…
同样,我可以尝试用一个字符串进行实例化,如:
$object = new 'App\MyClass';
抛出相同的错误.为什么是这样?
PHP语法在
zend_language_parser.y中定义,并不会为
原文链接:https://www.f2er.com/php/132671.htmlnew
运算符定义任何更复杂的表达式:
new_expr: T_NEW class_name_reference ctor_arguments { $$= zend_ast_create(ZEND_AST_NEW,$2,} | T_NEW anonymous_class { $$= $2; }
class_name_reference: class_name { $$= $1; } | new_variable { $$= $1; }
而new_variable
允许一组有限的变量表达式:
new_variable: simple_variable { $$= zend_ast_create(ZEND_AST_VAR,$1); } | new_variable '[' optional_expr ']' { $$= zend_ast_create(ZEND_AST_DIM,$1,$3); } | new_variable '{' expr '}' { $$= zend_ast_create(ZEND_AST_DIM,$3); } | new_variable T_OBJECT_OPERATOR property_name { $$= zend_ast_create(ZEND_AST_PROP,$3); } | class_name T_PAAMAYIM_NEKUDOTAYIM simple_variable { $$= zend_ast_create(ZEND_AST_STATIC_PROP,$3); } | new_variable T_PAAMAYIM_NEKUDOTAYIM simple_variable { $$= zend_ast_create(ZEND_AST_STATIC_PROP,$3); } // Copyright (c) 1998-2015 Zend Technologies Ltd.,the Zend license 2.00
这是为什么你不能在那里有字符串表达式. (它从未被扩展,因为简单的实例和$classvarnames通常是足够的,所以允许的语法大体上与PHP3相同,当它被介绍时).