Detailed explanation of the use of the two magic methods __call and __callStatic in php

黄舟
Release: 2023-03-14 12:38:01
Original
1161 people have browsed it

This article mainly introduces the __call and __callStatic methods of PHP magic methods. Friends in need can refer to the

core code


//魔术方法__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;);
Copy after login

You want to call my non-existent method t method
also passed a parameter

Array ( [0] => 1 [1] => 2 [2] => 3 )
Copy after login

You want to call my non-existent method say method
also passed a parameter

Array ( [0] => a [1] => b [2] => c )
Copy after login

You want to call my non-existent run static method
You also passed a parameter

Array ( [0] => d [1] => e [2] => f )
Copy after login

The above is the detailed content of Detailed explanation of the use of the two magic methods __call and __callStatic in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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!