PHP常用魔术方法(__call魔术方法:)

魔术方法  __call

<?php
//文件名:index.php
define('a',__DIR__);
include '/IMooc/Loader.php';
spl_autoload_register('\IMooc\Loader::autoload');


$Object = new IMoocObject();

echo $Object->test("哎哟喂",123);//在调用test不存在的方法时,会自动调用__call方法
/*输出:
    string(4) "test"
    array(2) {
          [0]=>string(9) "哎哟喂"
          [1]=>int(123)
    }
        试一试
*/
<?php
//文件名:Object.php

namespace IMooc; class Object { function __call($name, $arguments)//$name方法名=test,$arguments参数="哎哟喂",123 { var_dump($name,$arguments); return "试一试"; } }

静态魔术方法__callStatic


<?php
//文件名:index.php
define('a',__DIR__);
include '/IMooc/Loader.php';
spl_autoload_register('\IMooc\Loader::autoload');


$Object = new IMoocObject();
echo IMoocObject::test("hello1",1234);
/*输出:
string(4) "test"
array(2) {
[0]=>string(6) "hello1"
[1]=>int(1234)
}
试一试
*/
 
<?php
namespace IMooc;//文件名:Object.php
class Object
{
    //静态方法
    static function __callStatic($name, $arguments)//$name静态方法名=test,$arguments参数数组="哎哟喂",123
    {
        var_dump($name,$arguments);
        return "试一试";
    }
}
原文地址:https://www.cnblogs.com/shark1100913/p/5523083.html