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

巴扎黑
Release: 2023-03-07 14:54:01
Original
2140 people have browsed it

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

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!

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!