关于PHP中static和self的区别

本文详细阐述了PHP中static与self关键字的区别及使用场景。self关键字用于引用当前类的方法,而static则根据调用该方法的类来确定具体指向。通过实际代码示例展示了两者的不同之处。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     之前看过一次,但是忘了static和self的具体区别了,这次在复习一下。

     static是PHP5.3之后加进来的,看一下英文的解释:

    

     self refers to the same class whose method the new operation takes place in.

     static in PHP 5.3's late static bindings refers to whatever class in the hierarchy which you call the method on.

     之前看过一个文章这个例子很好:

class A {
    public static function who() {
        echo __CLASS__;
    }
    public static function test() {
        self::who();
//        static::who();
    }
}
A::test();

class B extends A {
    public static function who() {
        echo __CLASS__;
    }
}
echo B::test();

self:静态方法,指向本身存在的类

static:指向调用它的类

还是很容易理解的,下次不能再忘了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值