静态变量调用中static和self的区别 PHP

在php的静态变量调用中有static和self两个方法,它们的作用是不同的。static访问的是堆内存中的数据,代表的是当前实例化的类的本身,而self调用的是当前代码片段的这个类。
Demo和测试效果如下:   

<?php

class Usps {
    protected static $str = "Class:Usps";

    public static function printBySelf(){
        echo get_called_class()."<br>";
        echo self::$str."<br>";
    }

    public static function printByStatic(){

        echo get_called_class()."<br>";
        echo static::$str."<br>";
    }
}

class Priority extends Usps{
    protected static $str = "Class:Priority";
    public static function printDirect(){
        echo static::$str.'<br/>';
        echo self::$str.'<br/>';
    }
}

Priority::printBySelf();
Priority::printByStatic();
Priority::printDirect();

结果如下:

Guess you like

Origin blog.csdn.net/qq_24973351/article/details/109285089