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

高洛峰
Release: 2023-03-03 18:06:02
Original
1367 people have browsed it

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();
Copy after login

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();
Copy after login

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!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!