Home  >  Article  >  Backend Development  >  Detailed explanation of magic method __call() instance (php advanced object-oriented tutorial)

Detailed explanation of magic method __call() instance (php advanced object-oriented tutorial)

巴扎黑
巴扎黑Original
2017-04-18 18:16:532151browse

What is the __call() magic method?

__call is one of the magic methods. When the program calls a method that is not declared or has no permission to call in the current class, the __call method will be called. The __call() method contains two parameters, namely method name and method parameters. Among them, method parameters exist in the form of arrays.

Let’s give a simple example to help everyone understand:

<?php
header("content-type:text/html;charset=utf-8");
class Sport{
public function run(){
echo &#39;方法存在直接调用我&#39;;
}
function __call($name, $arguments)
{
echo &#39;方法不存在调用我&#39;;
echo &#39;<br/>&#39;;
echo &#39;方法名为:&#39;. $name;
echo &#39;<br/>&#39;;
echo &#39;参数为:<pre class="brush:php;toolbar:false">&#39;;
var_dump($arguments);
}
}
$sport = new Sport();
$sport ->run();
echo &#39;<br/>&#39;;
$sport ->football(&#39;贝克汉姆&#39;,30);

We create a class and create a run method and magic method __call() in the class. Next, we instantiate the object $sport and call two methods, one is the run() method that exists in the class, and the other is the football() method that does not exist. You can run the code to see the results.

The above is the detailed content of Detailed explanation of magic method __call() instance (php advanced object-oriented tutorial). For more information, please follow other related articles on 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