你不能做像$this->class::create() 这样的事情。如果您想访问静态成员,您必须使用Class::Member。 Zend PHP 解析器的当前实现仅支持直接在类名或变量上进行的静态方法调用。语法如下:
%token T_PAAMAYIM_NEKUDOTAYIM ":: (T_PAAMAYIM_NEKUDOTAYIM)"
function_call:
name argument_list
{ $$ = zend_ast_create(ZEND_AST_CALL, $1, $2); }
| class_name T_PAAMAYIM_NEKUDOTAYIM member_name argument_list
{ $$ = zend_ast_create(ZEND_AST_STATIC_CALL, $1, $3, $4); }
| variable_class_name T_PAAMAYIM_NEKUDOTAYIM member_name argument_list
{ $$ = zend_ast_create(ZEND_AST_STATIC_CALL, $1, $3, $4); }
| callable_expr argument_list
{ $$ = zend_ast_create(ZEND_AST_CALL, $1, $2); }
将类属性或方法声明为静态使它们无需实例化即可访问。声明为静态的属性不能被实例化的类对象访问(尽管静态方法可以)。
因为静态方法可以在没有创建对象实例的情况下调用,所以伪变量$this 在声明为静态的方法中不可用。
不能使用箭头运算符->通过对象访问静态属性。
静态属性示例
<?php
class Foo
{
public static $my_static = 'foo';
public function staticValue() {
return self::$my_static;
}
}
class Bar extends Foo
{
public function fooStatic() {
return parent::$my_static;
}
}
print Foo::$my_static . "\n";
$foo = new Foo();
print $foo->staticValue() . "\n";
print $foo->my_static . "\n"; // Undefined "Property" my_static
print $foo::$my_static . "\n";
$classname = 'Foo';
print $classname::$my_static . "\n"; // As of PHP 5.3.0
print Bar::$my_static . "\n";
$bar = new Bar();
print $bar->fooStatic() . "\n";
?>
静态方法示例
<?php
class Foo {
public static function aStaticMethod() {
// ...
}
}
Foo::aStaticMethod();
$classname = 'Foo';
$classname::aStaticMethod(); // As of PHP 5.3.0
?>
阅读更多:
http://php.net/manual/en/language.oop5.static.php
Call static method with class name stored as instance variable
https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L890