Home > Article > Backend Development > Detailed explanation of magic method __call() instance (php advanced object-oriented tutorial)
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 '方法存在直接调用我'; } function __call($name, $arguments) { echo '方法不存在调用我'; echo '<br/>'; echo '方法名为:'. $name; echo '<br/>'; echo '参数为:<pre class="brush:php;toolbar:false">'; var_dump($arguments); } } $sport = new Sport(); $sport ->run(); echo '<br/>'; $sport ->football('贝克汉姆',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!