本文介绍了PHP静态函数在动态环境中被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因为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及以上版本总是可能的。
另外,它在文档中提到()
而不是错误()
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)
And this not a bug (static methods assigned to instances)
这篇关于PHP静态函数在动态环境中被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!