Is there a way to have PHP subclasses inherit properties (both static and instance)?
Asked Answered
F

3

4

If I declare a base class as follows:

abstract class Parent {

  protected static $message = "UNTOUCHED";

     public static function yeah() {
         static::$message = "YEAH";
     }
     public static function nope() {
         static::$message = "NOPE";
     }

     public static function lateStaticDebug() {
         return(static::$message);
     }

}

and then extend it:

class Child extends Parent {
}

and then do this:

Parent::yeah();
Parent::lateStaticDebug();  // "YEAH"

Child::nope();
Child::lateStaticDebug();  // "NOPE"

Parent::yeah();
Child::lateStaticDebug()   // "YEAH"

Is there a way to have my second class that inherits from the first also inherit properties and not just methods?

I'm just wondering if there's something about PHP's late static binding and also inheritance that would allow for this. I'm already hacking my way around this...But it just doesn't seem to make sense that an undeclared static property would fall back on its parent for a value!?

Filter answered 30/7, 2009 at 0:24 Comment(3)
I have created the following PHP bug report: bugs.php.net/49105 I encourage people to go check it out and lend their support. This is a pretty restrictive limitation. The current behaviour is also completely pointless.Filter
It seems as though people have resigned themselves to the default behaviour which is not intuitive. The problem here is either that PHP has an incorrect default behaviour, or there needs to be a way to dynamically declare properties.Filter
I totally support you in your quest, this behaviour is very annoying ! Unfortunately it seems to be here to stay.Impermeable
F
-3

The answer is "with a workaround".

You have to create a static constructor and have it called to copy the property.

Filter answered 7/8, 2009 at 16:31 Comment(3)
Sadly this was to my blog on blogspot which I took down a while ago. I apologize.Filter
The workaround was to create a static constructor that copies the property manually and call it.Filter
that doesn't solve the actual situation from the question: usage in static contexts.Costly
A
0

Inheritance and static properties does sometime lead to "strange" things in PHP.

You should have a look at Late Static Bindings in the PHP's manual : it explains what can happen when inheriting and using static properties in PHP <= 5.2 ; and gives a solutions for PHP >= 5.3 where you can use the static:: keywords instead of self::, so that static binding is done at execution (and not compile) time.

Arnelle answered 30/7, 2009 at 4:53 Comment(1)
I already use late static binding in my example... I'm aware of what it is.Filter
C
-1

To those that end up here wondering "WTF PHP", it seems there are a couple of reasons for this behavior and why it was kept, albeit weird:

  1. static properties will always use the same memory reference, just like static vars do (mentioned in the bug the OP raised)
  2. that same reference is shared between classes and subclasses (explained in a duplicated bug)
  3. it seems to be useful in some other contexts, so it isn't a "full" bug, just undocumented behavior. If it got "fixed", it would then cause compatibility issues with previously working code (a backwards-compatibility break).

There are two questions left, though:

  • why wouldn't late static bindings change that: probably related to #1
  • why that drawback we see isn't explained in the doc pages................ Well, that's PHP, right?
Costly answered 5/6, 2021 at 2:55 Comment(0)
F
-3

The answer is "with a workaround".

You have to create a static constructor and have it called to copy the property.

Filter answered 7/8, 2009 at 16:31 Comment(3)
Sadly this was to my blog on blogspot which I took down a while ago. I apologize.Filter
The workaround was to create a static constructor that copies the property manually and call it.Filter
that doesn't solve the actual situation from the question: usage in static contexts.Costly

© 2022 - 2024 — McMap. All rights reserved.