Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending PHP static classes

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!

like image 315
treznik Avatar asked Aug 14 '09 22:08

treznik


People also ask

Can we extend static class in PHP?

With this done, you can finally extend static methods without having to re-declare methods. As mentioned in Chacha102's answer, PHP 5.3 also has the get_called_class() function which makes this workaround superfluous.

Can we inherit static class in PHP?

In PHP, if a static attribute is defined in the parent class, it cannot be overridden in a child class.

What is a PHP static class?

Introduction: A static class in PHP is a type of class which is instantiated only once in a program. It must contain a static member (variable) or a static member function (method) or both. The variables and methods are accessed without the creation of an object, using the scope resolution operator(::).

How use $this in static method in PHP?

In the static method,properties are for the class, not the object. This is why access to static methods or features is possible without creating an object. $this refers to an object made of a class, but $self only refers to the same class.


2 Answers

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__;
    }
}
like image 79
Andrew Moore Avatar answered Sep 26 '22 09:09

Andrew Moore


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:

class Super {
    public static function get_class1_name()        
    {
        echo __CLASS__;
    }
    public static function get_title()
    {
        echo get_called_class();
    }

}
class Sub extends Super {    
    public static function get_class2_name()        
    {
        echo __CLASS__;
    }

}
Sub::get_title(); // Echos Sub.
Sub::get_class1_Name(); // echos super
Sub::get_class2_Name(); // echos sub

Therefore you don't have to declare any variables.

like image 43
Tyler Carter Avatar answered Sep 25 '22 09:09

Tyler Carter