PHP 可以用 static:: 替换 self:: 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4718808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:07:55  来源:igfitidea点击:

PHP Can static:: replace self::?

phpstatic-methodsstatic-members

提问by elite5472

I am a little confused with this matter. I am designing an ORM class that tries to behave very similarly to ActiveRecord in ruby on rails, but that's beside the point.

我对这个问题有点困惑。我正在设计一个 ORM 类,该类的行为与 ruby​​ on rails 中的 ActiveRecord 非常相似,但这不是重点。

What I'm trying to say is that my class makes extensive use of static attribute inheritance, specially for database and table handling. My question is, should I use self:: at all?

我想说的是,我的类广泛使用了静态属性继承,特别是用于数据库和表处理。我的问题是,我应该使用 self:: 吗?

回答by DarkThrone

You have to ask yourself: "Am I targeting the problem with the adequated approach?"

您必须问自己:“我是否使用适当的方法针对问题?”

self::and static::do two different things. For instance self::or __CLASS__are references to the current class, so defined in certain scope it will NOT suffice the need of static calling on forward.

self::static::做两件不同的事情。例如self::or__CLASS__是对当前类的引用,因此在某些范围内定义它不足以满足静态调用转发的需要。

What will happen on inheritance?

继承会发生什么?

class A {
    public static function className(){
        echo __CLASS__;
    }

    public static function test(){
        self::className();
    }
}

class B extends A{
    public static function className(){
        echo __CLASS__;
    }
}

B::test();

This will print

这将打印

A

In the other hand with static::It has the expected behaviour

另一方面,static::它具有预期的行为

class A {
    public static function className(){
        echo __CLASS__;
    }

    public static function test(){
        static::className();
    }
}

class B extends A{
    public static function className(){
        echo __CLASS__;
    }
}


B::test();

This will print

这将打印

B

That is called late static bindingin PHP 5.3.0. It solves the limitation of calling the class that was referenced at runtime.

在 PHP 5.3.0 中称为后期静态绑定。它解决了在运行时调用被引用的类的限制。

With that in mind I think you can now see and solve the problem adequately. If you are inheriting several static members and need access to the parent and child members self::will not suffice.

考虑到这一点,我认为您现在可以充分了解并解决问题。如果您继承了多个静态成员并且需要访问父成员和子成员是self::不够的。

回答by Radouane FADEL

try to use the code bellow to see the difference between selfand static:

尝试使用下面的代码来查看selfstatic之间的区别:

<?php
class Parent_{
    protected static $x = "parent";
    public static function makeTest(){
        echo "self => ".self::$x."<br>";
        echo "static => ".static::$x;       
    }
}

class Child_ extends Parent_{
    protected static $x = "child";
}

echo "<h4>using the Parent_ class</h4>";
Parent_::makeTest();

echo "<br><h4>using the Child_ class</h4>";
Child_::makeTest();
?>

and you get this result:

你会得到这个结果:

using the Parent_ class

  • self => parent
  • static => parent

using the Child_ class

  • self => parent
  • static => child

使用 Parent_ 类

  • 自己 => 父母
  • 静态 => 父

使用 Child_ 类

  • 自己 => 父母
  • 静态 => 孩子