Static members and inheritance in php
In PHP, if a static attribute is defined in the parent class, it cannot be overridden in a child class. But I'm wondering if there's any way around this. Show I'm trying to write a wrapper for someone else's (somewhat clunky) function. The function in question can be applied to lots of different data types but requires different flags and options for each. But 99% of the time, a default for each type would suffice. It would be nice if this could be done with inheritance, without having to write new functions each time. For example:
hakre 187k48 gold badges418 silver badges801 bronze badges asked Feb 10, 2009 at 15:19
PartialOrderPartialOrder 2,7573 gold badges34 silver badges43 bronze badges Actually I think it is not true: you can ovverride static propeties (you need >=5.3 PHP for that). But you have to be careful when refrencing for that static property (and this is the mistake in the original code) You need to use static::$myStaticProperty instead of using self::$myStaticProperty self:: will refrence to the current class so if you are inside an inherited static method this will refrence the static property of that class defined that method! While using reference keyword static:: will act like $this - when you are using instance methods/propeties. doSomething() is an inherited static method in class Bar in your example. Since you used self:: there, it will reference to the static property of class Foo. This is the reason why you didn't see any difference... Try to change self:: to static::! Here is a code example - I used it myself to test those things. We have static property/method inheritance, override and value change in it - run it and you will see the result!
This will output: -- testing class A: And here we are, you can see the difference between overriden and only value overwritten static properties... look the output line I marked with bold! When we invoked the modStaticProperty() of class B, it changed the value of that static variable in class A too. Since that static property was inherited and was not overriden! Think about it... answered Feb 17, 2013 at 9:53
2 The forthcoming PHP 5.3.0 release includes late static binding, which might help. Using this feature you could use a static variable inside a static method, and let the late static binding take care of finding the "right" method.
answered Feb 10, 2009 at 15:29
Paul DixonPaul Dixon 290k50 gold badges309 silver badges344 bronze badges 1 Classic example of why using statics as globals (functions in this case) is a bad idea no matter the language. The most robust method is to create multiple implementation sub classes of an abstract base "Action" class. Then to try and remove some of the annoyance of instantiating an instance of the class just to call it's methods, you can wrap it in a factory of some sort. For example:
Then create a factory to "aid" in instantiation of the function
Then use it as:
answered Feb 10, 2009 at 15:30
Allain LalondeAllain Lalonde 89.5k70 gold badges184 silver badges238 bronze badges Not the answer you're looking for? Browse other questions tagged php inheritance or ask your own question.What is static inheritance in PHP?In PHP, if a static attribute is defined in the parent class, it cannot be overridden in a child class.
Can static members be inherited?In essence, static members are not inherited, they are just class-level (i.e. universal) methods that are accessible from anywhere.
What is static member in PHP?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(::).
Can we inherit static method in PHP?PHP implements a feature called late static bindings which can be used to reference the called class in a context of static inheritance. More precisely, late static bindings work by storing the class named in the last "non-forwarding call".
|