views:

343

answers:

2

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!?

A: 

Hi,

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.

Pascal MARTIN
I already use late static binding in my example... I'm aware of what it is.
Omega
A: 

The answer is "with a workaround".

Please see this link which contains two workarounds and an explanation on the matter.

Omega
the link is not working
Elzo Valugi
Sadly this was to my blog on blogspot which I took down a while ago. I apologize.
Omega