IE盒子

搜索
查看: 119|回复: 0

记录工作中遇到的坑:php中static与self的区别?魔术方法 ...

[复制链接]

2

主题

7

帖子

11

积分

新手上路

Rank: 1

积分
11
发表于 2023-4-3 07:43:50 | 显示全部楼层 |阅读模式
PHP中的魔术方法想必大家都用过,在一些框架中经常用到
平时工作上可能都是业务开发比较多,一些基础知识难免会忘记,接下就讲下我遇到的坑
以下代码运行结果是
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();

想都不用想,直接是运行结果是打印 “进来了”
在我电脑上自己跑了下,结果让自己打脸了
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:: 不再被解析为定义当前方法所在的类,而是在实际运行时计算的。也可以称之为“静态绑定”,因为它可以用于(但不限于)静态方法的调用。在有继承的父类与子类中,这个派上用场了
来源:记录工作中遇到的坑:php中static与self的区别?魔术方法__callStatic 的用法
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表