Blogger Information
Blog 29
fans 0
comment 0
visits 27236
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
魔术方法2:__call() 和 __callStatic()
LIWEN的博客
Original
733 people have browsed it

 __call() 和 __callStatic()两个魔术方法的使用:
1、__call($name,$argc):当在类外部调用一个不存在的动态方法的时候,自动调用。
2、__callStatic($name,$argc):当在类外部调用一个不存在的静态方法的时候,自动调用。

本例中调用的两个方法在demo.php文件中。

执行结果如下图:

2018-01-12_105732.png

魔术方法调用代码文件如下:

<?php
header('content-type:text/html;charset=utf-8');
require 'demo.php';
class Demo
{
    public function __call($name,$arguments)
    {
        return call_user_func_array([(new Demo2()),$name],$arguments);
    }
    public static function __callStatic($name, $arguments)
    {
        return call_user_func_array(['Demo2',$name],$arguments);
    }
}
$obj = new Demo();
echo $obj->hello('php','中文网','www.php.cn');
echo '<hr>';
echo Demo::get('Peter','讲师');

demo.php文件代码如下:

<?php
/**
 * 方法实际所在的文件
 */
header('content-type:text/html;charset=utf-8');
class Demo2
{
    public function hello($arg1,$arg2,$arg3)
    {
        return '<h3>我是hello方法,我在demo.php中</h3>'.$arg1.$arg2.'('.$arg3.')';
    }
    public static function get($name,$job)
    {
        return '<h3>我是get方法,我也在demo.php中</h3>'.$name.'是'.$job;
    }
}


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post