首頁  >  文章  >  後端開發  >  PHP魔術方法之__call與__callStatic如何使用

PHP魔術方法之__call與__callStatic如何使用

墨辰丷
墨辰丷原創
2018-05-19 09:50:081640瀏覽

這篇文章主要介紹了PHP魔術方法之__call與__callStatic如何使用,需要的朋友可以參考下

核心程式碼


##

//魔术方法__call 
/* 
$method 获得方法名 
$arg 获得方法的参数集合 
*/
class Human {
 private function t(){

 }

 public function __call($method,$arg){
  echo &#39;你想调用我不存在的方法&#39;,$method,&#39;方法<br/>&#39;; 
  echo &#39;还传了一个参数<br/>&#39;; 
  echo print_r($arg),&#39;<br/>&#39;; 
 }

 public static function __callStatic($method,$arg){
  echo &#39;你想调用我不存在的&#39;,$method,&#39;静态方法<br/>&#39;; 
  echo &#39;还传了一个参数<br/>&#39;; 
  echo print_r($arg),&#39;<br/>&#39;; 
 }
}


$ha = new Human();

//example1
$ha->t(1,2,3);

echo &#39;<br>&#39;;
//example2
$ha->say(&#39;a&#39;,&#39;b&#39;,&#39;c&#39;);

echo &#39;<br>&#39;;
//example3
$ha::run(&#39;d&#39;,&#39;e&#39;,&#39;f&#39;);


你想呼叫我不存在的方法t方法

也傳了一個參數
Array ( [0] => 1 [1] => 2 [2] => 3 )

你想呼叫我不存在的方法say方法

也傳了一個參數
Array ( [0] => a [1] => b [2 ] => c )

你想呼叫我不存在的run靜態方法

也傳了一個參數
Array ( [0] => d [1] => e [ 2] => f )

相關推薦:

php常用的魔術方法

# #【php類別與物件】

魔術方法

#PHP常用

魔術方法的效能探究

以上是PHP魔術方法之__call與__callStatic如何使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn