在PHP的非静态方法中可以调用静态方法
class test{ public static function strPrint(){ echo 'this is strPrint static function<br>'; } public function staticFuncInvoke(){ self::strPrint(); } } $test = new test(); $test->staticFuncInvoke();
上面的代码会输出: this is strPrint static function.
而下面的代码会直接挂掉,php直接给出fatal error:
Fatal error: Using $this when not in object context in E:\htdocs\test\content.php on line 6
class test{ public static function strPrint(){ $this->staticFuncInvoke(); } public function staticFuncInvoke(){ echo 'this is a nonstatic function named staticFuncInvoke'; } } test::strPrint();
以上就是小编为大家带来的浅谈PHP中静态方法和非静态方法的相互调用全部内容了,希望大家多多支持呐喊教程~