温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

回顾php魔术方法__call(),__callStatic()

发布时间:2020-06-18 20:33:29 来源:网络 阅读:770 作者:hgditren 栏目:web开发
<?php declare(strict_types=1);//开启强类型模式 //不可访问的方法:private/protected/不存在的方法 class Person{     public function say(){         echo "Hello world";         echo "\r\n";     } } (new Person())->say();//调用类中存在的方法 (new Person())->eat('food');//调用类中不可访问的方法


调用类中不存在的方法 PHP Fatal error:  Uncaught Error: Call to undefined method Person::eat() in /home/zrj/www/zhangrenjie_test/test/36.php:26 Stack trace: #0 {main}   thrown in /home/zrj/www/zhangrenjie_test/test/36.php on line 26



class Person {     public function say()     {         echo "Hello world";         echo "\r\n";     }     // 在对象中调用一个不可访问方法时,__call() 会被调用。     public function __call($functionName, $arguments)     {         echo "您调用了类中不存在的方法:" . $functionName . "\r\n";         echo "接受的参数为:" . print_r($arguments, true);     } } (new Person())->say(); (new Person())->eat('food', 'chicken', 'bull');


Hello world

您调用了类中不存在的方法:eat

接受的参数为:Array

(

    [0] => food

    [1] => cocal

    [2] => bull

)


class Person {     public function __call(string $name, array $arguments)     {         echo "Call not exists dynamic method :" . $name . "\r\n";         echo $name . " : " . $arguments[0] . "\r\n\r\n";     }     /**  PHP 5.3.0之后版本  */     public static function __callStatic(string $name, array $arguments)     {         echo "Call not exists static method :" . $name . "\r\n";         echo $name . " : " . $arguments[0] . "\r\n\r\n";     } } (new Person())->say('hello world'); (new Person())->__call('say', ['hello world']); Person::do('coding php'); Person::__callStatic('do', ['coding java']);


Call not exists dynamic method :say
say : hello world

Call not exists dynamic method :say
say : hello world

Call not exists static method :do
do : coding php

Call not exists static method :do
do : coding java

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI