Home >Backend Development >PHP Tutorial >A brief discussion on the mutual calling of static methods and non-static methods in PHP

A brief discussion on the mutual calling of static methods and non-static methods in PHP

高洛峰
高洛峰Original
2016-12-20 16:49:441390browse

Static methods can be called in non-static methods of PHP

class test{
   
  public static function strPrint(){
    echo &#39;this is strPrint static function<br>&#39;;
  }
   
  public function staticFuncInvoke(){
    self::strPrint();
  }
}
 
$test = new test();
 
$test->staticFuncInvoke();

The above code will output: this is strPrint static function.

And the following code will hang directly, and PHP will directly give fatal error:

Fatal error: Using $this when not in object context in E:htdocstestcontent.php on line 6

class test{
   
  public static function strPrint(){
    $this->staticFuncInvoke();
  }
   
  public function staticFuncInvoke(){
    echo &#39;this is a nonstatic function named staticFuncInvoke&#39;;
  }
}
 
test::strPrint();

The above is the editor’s brief discussion on the mutual calling of static methods and non-static methods in PHP That’s all. I hope everyone will support the PHP Chinese website~

For more articles on the mutual calling of static methods and non-static methods in PHP, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn