您的位置 首页 php

记录工作中遇到的坑:php中static与self、魔术方法static

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让外部不能访问,将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、魔术方法static

文章地址:https://www.zhihuclub.com/150363.shtml

关于作者: 智云科技

热门文章

网站地图