扩展 PHP 静态类
我在这方面已经挣扎了好几天了,我已经得出了一个结论,但是由于这个结论不是我想要的,所以在我放弃之前,我会尝试看看其他人是怎么说的。信仰最后消亡……
假设我们有一个超类(称为“Super”)和一个子类(称为“Sub”)。
class Super {
protected static $title = 'super';
public static function get_class_name()
{
echo __CLASS__;
}
public static function get_title()
{
echo self::$title;
}
}
class Sub extends Super {
protected static $title = 'sub';
}
现在,您可能会期望,由于 Sub 扩展了 Super,因此 Sub 现在将继承 Super 的所有方法,但是,它似乎只接收对 Sub 方法的引用。
我这样说是因为如果我调用:
Sub::get_class_name();
输出是“Super”,而不是“Sub”。
如果我
Sub::get_title();
再次调用:,输出是“super”,我什至在 Sub 中声明了 $title。
所以这意味着当我调用继承的静态函数时,该函数的范围将是超类,而不是被调用的范围(即使您打印回溯,它也会显示调用是在超类上进行的!!!),为了获得调用子类的范围,我需要在该子类中重新声明该方法。 这种方式违背了扩展类的目的,不是吗?
所以我的问题是,我可以扩展一个静态类,调用继承的方法之一并拥有子类的作用域吗?或者至少能够识别它的类名? 如果不是,我为什么要扩展静态类?
谢谢!
I've been struggling in this area for days now, and I have reached a conclusion, but since the conclusion was not what I was looking for, before I give up, I'll try to see what other people say. Faith dies last...
Let's say we have a superclass (called "Super") and a subclass (called "Sub").
class Super {
protected static $title = 'super';
public static function get_class_name()
{
echo __CLASS__;
}
public static function get_title()
{
echo self::$title;
}
}
class Sub extends Super {
protected static $title = 'sub';
}
Now, you would probably expect since Sub extends Super, that Sub would now inherit all of Super's methods, however, it seems to only receive references to the Sub's methods.
I say this because if I call:
Sub::get_class_name();
the output is "Super", and not "Sub".
And if I call:
Sub::get_title();
again, the output is "super", and I even have the $title declared in Sub.
So this means that when I call an inherited static function, the function's scope will be the super class, not the one called upon (even if you print the backtrace, it will show that the call was made on the superclass!!!), and in order to obtain the scope as the subclass that the call is being made upon, I need to redeclare that method inside that subclass. Well this kind of defeats the purpose of extending classes, don't it?
So my question is, can I ever extend a static class, call one of the inherited methods and have the subclass's scope? or at least to be able to identify it's classname?
And if not, why would I ever want to extend static classes?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
同样,在 PHP 5.3.0 之前这是不可能的。
晚期静态绑定 是在 PHP 5.3.0 中引入的,它允许您准确地执行您想要的操作想要通过
static
关键字。get_class_name()
仍将返回Super
,尽管__CLASS__
始终返回正在运行的方法声明所在的当前类(类似于__FILE__
总是返回当前文件,无论您是否包含它)。为此,您别无选择,只能在
Sub
类中重新声明该函数。Again, this is not possible prior to PHP 5.3.0.
Late Static Binding was introduced in PHP 5.3.0 and allows you to do exactly what you want via the
static
keyword.get_class_name()
will still returnSuper
though has__CLASS__
always returns the current class the method being run is declared in (kind of like__FILE__
which always returns the current file no matter if you included it or not).For that you don't have any choice but to re-declare the function in the
Sub
class.您可以使用 get_used_class() 来获取您正在调用的类的类名,即使它是静态的。您不必在任何地方声明它。
来自安德鲁的示例:
因此您不必声明任何变量。
You can used
get_called_class()
to get the class name of the class you are calling, even if it is static. You don't have to declare it anywhere.From Andrew's Example:
Therefore you don't have to declare any variables.
幸运的是,我正在为我做一些事情,所以我说,管它呢,我正在使用 PHP5.3。但即便如此,我不喜欢我必须在每个类中重新声明“get _class _name”,也许我要扩展 10 个类。所以我想出了这个解决方案:
它可能看起来有点脏,但我不认为它有那么糟糕。完成此操作后,您最终可以扩展静态方法,而无需重新声明方法。
Fortunately, I'm doing something for me, so I said, screw it, I'm using PHP5.3. But even so, I don't like that I have to redeclare "get _class _name" in every class, maybe I'm extending like 10 classes. So I came up with this solution:
It might seem a little dirty, but I don't think it's that bad. With this done, you can finally extend static methods without having to re-declare methods.