PHP静态函数在动态环境中被调用 [英] PHP static function being called in dynamic environment
本文介绍了PHP静态函数在动态环境中被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用php 5.3.2
class weird {
public static function iamstatic($ calledFrom){
echoI一个静态函数调用一个$ calledFrom operator\\\
;
}
public function test(){
self :: iamstatic(static);
$ this-> iamstatic(dynamic);
}
}
$ c = new weird();
$ c-> test();
weird :: iamstatic(Static outside class);
$ c-> iamstatic(Dynamic outside class);
输出:
我是一个使用静态运算符调用的静态函数
我是一个使用动态运算符调用的静态函数
我是一个使用静态外部类运算符调用的静态函数
我是一个使用动态外部类运算符调用的静态函数
解决方案
php5.0及以上版本总是可能的。
另外,它在文档中提到( static )
声明类属性或方法作为静态使得它们可以访问
,而不需要实例化该类。声明为
static的属性无法使用实例化的类对象(尽管
静态方法可以)访问。
而不是错误(分配给实例的静态方法)
Since when PHP allows to call static function like a dynamic function?
I am using php 5.3.2
class weird{
public static function iamstatic($calledFrom){
echo "I am a static function called with a $calledFrom operator\n";
}
public function test(){
self::iamstatic("static");
$this->iamstatic("dynamic");
}
}
$c = new weird();
$c->test();
weird::iamstatic("Static outside class");
$c->iamstatic("Dynamic outside class");
This outputs :
I am a static function called with a static operator
I am a static function called with a dynamic operator
I am a static function called with a Static outside class operator
I am a static function called with a Dynamic outside class operator
解决方案
It was always possible for php5.0 and above.
Also, it's mentioned in documentation (static)
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static cannot be accessed with an instantiated class object (though a static method can).
And this not a bug (static methods assigned to instances)
这篇关于PHP静态函数在动态环境中被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文