首页 » 网站推广 » __callstaticphp技巧_记录工作中碰着的坑php中static与self魔术方法static

__callstaticphp技巧_记录工作中碰着的坑php中static与self魔术方法static

访客 2024-10-23 0

扫一扫用手机浏览

文章目录 [+]

平时事情上可能都是业务开拓比较多,一些根本知识难免会忘却,接下就讲下我碰着的坑

以下代码运行结果是

__callstaticphp技巧_记录工作中碰着的坑php中static与self魔术方法static

class Base{ protected static $instance; public static function make($rules = [], $message = [], $field = []) { if (is_null(self::$instance)) { self::$instance = new self($rules, $message, $field); } return self::$instance; } public static function __callStatic($method, $params) { $class = self::make(); if (method_exists($class, $method)) { return call_user_func_array([$class, $method], $params); } else { throw new \BadMethodCallException('method not exists:' . __CLASS__ . '->' . $method); } } public function show(){ var_dump("进来了"); }}Base::show();

__callstaticphp技巧_记录工作中碰着的坑php中static与self魔术方法static
(图片来自网络侵删)

想都不用想,直接是运行结果是打印 “进来了”

在我电脑上自己跑了下,结果让自己打脸了

Strict Standards: Non-static method Base::show() should not be called statically in C:\phpStudy\PHPTutorial\WWW\caiji3\test.php on line 28

说不是一个静态方法,调用办法没对,证明没有进入callStatic 方法

这是为什么,难道是php版本没对???

Base::show()是静态调用,但show()不是static,语法上就出错了。
其余,__callStatic()是说,当你调用一个不可访问的static方法时,自动触发。
作甚不可访问?你类外调用没有权限或者不存在。
类似的,__call()是说,当你调用一个不可访问的普通方法时,自动触发。
因此,你要实现_callStatic()的触发,得确保类外是静态调用一个不可访问的静态方法

对了,要让Base中的show让外部不能访问,将public 改为protected

class Base{ protected static $instance; public static function make($rules = [], $message = [], $field = []) { if (is_null(self::$instance)) { self::$instance = new self($rules, $message, $field); } return self::$instance; } public static function __callStatic($method, $params) { $class = self::make(); if (method_exists($class, $method)) { return call_user_func_array([$class, $method], $params); } else { throw new \BadMethodCallException('method not exists:' . __CLASS__ . '->' . $method); } } protected function show(){ var_dump("进来了"); }}Base::show();

在看以下代码

class Base{ protected static $instance; public static function make($rules = [], $message = [], $field = []) { if (is_null(self::$instance)) { self::$instance = new self($rules, $message, $field); } return self::$instance; } public static function __callStatic($method, $params) { $class = self::make(); if (method_exists($class, $method)) { return call_user_func_array([$class, $method], $params); } else { throw new \BadMethodCallException('method not exists:' . __CLASS__ . '->' . $method); } } }class ActivityRewardServiceextends Service{ protected function test1(){ dump("进来了不"); }}RewardService::test1();

上面实行又报错?

Non-static method ActivityRewardService::test1() should not be called statically

callStatic 在base中,也便是 RewardService::test1()会直接进入Base中,new Self 实例化的是Base,并没有RewardService

将new self 改成new static 就对了...

总结:

__callStatic() 在一个静态的高下文中,如果调用的方法不能访问,它将被触发

利用 self:: 或者 __CLASS__ 对当前类的静态引用,取决于定义当前方法所在的类:

利用 static:: 不再被解析为定义当前方法所在的类,而是在实际运行时打算的。
也可以称之为“静态绑定”,由于它可以用于(但不限于)静态方法的调用。
在有继续的父类与子类中,这个派上用场了

(此处已添加小程序,请到今日头条客户端查看)
标签:

相关文章

招商蛇口中国房地产龙头企业,未来可期

招商蛇口(股票代码:001979),作为中国房地产企业的领军企业,自成立以来始终秉持“以人为本,追求卓越”的经营理念,致力于打造高...

网站推广 2025-02-18 阅读1 评论0