简体   繁体   English

PHP 5和严格标准错误

[英]PHP 5 and Strict Standards error

Why I get this error?: 为什么会出现此错误?:

Strict Standards: Non-static method Hello_Misc::fix_protocol() should not be called statically, assuming $this from incompatible context in test.php on line 834 严格标准:非静态方法Hello_Misc :: fix_protocol()不应静态调用,假设$ this来自test.php上834行的不兼容上下文

line 834: 834行:

$this->my_url = Hello_Misc::fix_protocol($my, 1);

I am using latest version of PHP (>= 5.1.3) but in previous version (<= 5.1.2) it does work ( ref ). 我正在使用最新版本的PHP(> = 5.1.3),但在以前的版本(<= 5.1.2)中,它确实可以工作( ref )。

The static keyword is the key! static关键字是关键! You need to declare the method as static: 您需要将方法声明为静态方法:

class Hello_Misc {

   static public function fix_protocol($p1, $p2) {
      // code
   }

}

However, it you do so (and you should), you must make sure you're not using object context (ie $this ) in this method. 但是,如果这样做(并且应该这样做),则必须确保在此方法中未使用对象上下文(即$this )。 If you do, you will have to review your design a little. 如果这样做,您将不得不稍微回顾一下您的设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM