扩展PHP静态类 [英] Extending PHP static classes

查看:99
本文介绍了扩展PHP静态类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在这个地区努力了几天,我得出了一个结论,但由于结论并不是我想要的,在我放弃之前,我会试着看看别人说什么。



假设我们有一个超类(称为超级)和一个子类(称为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。 / p>

所以这意味着当我调用一个继承的静态函数时,函数的作用域将是超类,而不是被调用的(即使你打印backtrace,显示调用是在超类!!!),并且为了获得作为子类的作用域作为调用的范围,我需要重新声明那个子类中的方法。



所以我的问题是,我可以扩展一个静态类,调用一个继承的方法,并有子类的范围?或至少能够识别它的类名?
如果不是,为什么我想要扩展静态类?



谢谢!

解决方案

同样,这在PHP 5.3.0之前是不可能的。



晚期静态绑定 在PHP 5.3.0中引入,并允许您通过 static 关键字。

  class Super {
protected static $ title ='super'
public static function get_class_name()
{
echo __CLASS__;
}
public static function get_title()
{
echo static :: $ title;
}
}
class Sub extends Super {
protected static $ title ='sub';
}

get_class_name()仍然会返回 Super 虽然有 __ CLASS __ 总是返回当前类被运行的方法声明在(类似于 __ FILE __ ,它总是返回当前文件,无论是否包含)。



只能在 Sub 类中重新声明该函数。

  class Super {
protected static $ title ='super';
public static function get_class_name()
{
echo __CLASS__;
}
public static function get_title()
{
echo static :: $ title;
}
}
class Sub extends Super {
protected static $ title ='sub';

public static function get_class_name()
{
echo __CLASS__;
}
}


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!

解决方案

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.

class Super {
    protected static $title = 'super';
    public static function get_class_name()        
    {
        echo __CLASS__;
    }
    public static function get_title()
    {
        echo static::$title;
    }
}
class Sub extends Super {
    protected static $title = 'sub';
}

get_class_name() will still return Super 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.

class Super {
    protected static $title = 'super';
    public static function get_class_name()        
    {
        echo __CLASS__;
    }
    public static function get_title()
    {
        echo static::$title;
    }
}
class Sub extends Super {
    protected static $title = 'sub';

    public static function get_class_name()        
    {
        echo __CLASS__;
    }
}

这篇关于扩展PHP静态类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆