dro60731 2018-12-23 00:59
浏览 107

在php对象中,为什么静态变量只能用:: notation访问,但静态方法可以从::& - >调用

In PHP, for e.g. you define a variable & method static in a class. For its objects, why we can only access variables with :: notation while we can run static methods with -> or :: both? Why this dual behaviour?

class first {
    //variable
    public static $var=5;

    //method
    static function new(){
        echo "<br>";
        echo self::$var;
        echo "<br>";
    }
}

class second {

}

$obj = new first();

echo $obj->$var; // this throws an error
echo $obj::$var; // this runs
$obj->new(); // this also runs
$obj::new(); // this runs
  • 写回答

1条回答

  • dongmei2957 2018-12-23 10:21
    关注

    Although arbitrary, a static class variable only belongs to a class, not to an object. A static class method belongs to both a class and an object of that class.

    As demonstrated below, changing a static variable of a class would change the variable in all instances of that class. So the -> notation would be deceitful. This does not happen with a static class method.

    Imagine that -> would be allowed on static variables, then

    calling $someObjectOfTypeX->some_static_variable = 'some_value' would change the state of $anotherObjectOfTypeX.

    Calling $someObjectOfTypeX->someStaticFunction() however, does not change the state of $anotherObjectOfTypeX.

    Although the keyword static is identical it has different implications for functions and variables. Static variables are shared by all instances of a class. Static function don't alter the objects state and therefore also will not change the state of other instances of the same class.

    <?php
    
    class first
    {
        //variable
        public static $var = 5;
    
        //method
        static function new()
        {
            echo "<br>";
            echo self::$var;
            echo "<br>";
        }
    }
    
    $obj1 = new first();
    $obj2 = new first();
    
    echo $obj1->var; // this throws an error
    echo $obj1::$var; // this runs
    echo first::$var; // this runs
    
    $obj2::$var = 10; // changes $var in class first (both object $obj1 and object $obj2)
    $obj2->var = 15; // this throws an error (if it didn't it would change the variable also in $obj1)
    
    $obj1->new(); // this also runs
    $obj1::new(); // this runs
    first::new(); // this runs
    
    评论

报告相同问题?

悬赏问题

  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题