浅谈PHP中静态方法和非静态方法的相互调用
时间:2020-10-07 14:27:02|栏目:PHP代码|点击: 次
在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();