程式碼如下:
<?php
class Demo
{
public function testing()
{
echo "testing\n";
}
}
Demo::testing();
php7.0 執(zhí)行輸出:
$ php demo.php
testing
php5.6 執(zhí)行輸出
$ php demo.php
PHP Strict Standards: Non-static method Demo::testing() should not be called statically in /home/runner/Code/funny/demo.php on line 11
testing
還有這種操作 ?? 原理是啥 ??
5.6 版本以下的 類別假如無需實例化而直接呼叫方法與屬性,該方法與屬性必須是靜態(tài)方法,即Demo類別要直接呼叫testing方法,testing必須 public static function testing() { }。不然就會報錯。
我不知道7.0以上是不是可以不用聲明
看到了, 只是 php-cli 會執(zhí)行, 在php-fpm 還是不會
Deprecated: Non-static method Demo::testing() should not be called statically in test.php on line 11
testing
可以認為是PHP的實現(xiàn)不嚴謹.
如果非static方法裡面沒有使用$this, 可以用::呼叫.