0

How I can Late Bind a Static Field to a inherited class.

I have tried following:

<?php
interface StaticInitializier {
  public static function StaticInit();
}


abstract class ParentClass implements StaticInitializier {
  protected static $_foo;

  public static function Test() {
  echo '<p>' . static::$_foo . '</p>';
  }
}


class FirstClass extends ParentClass {
  public static function StaticInit()
  {
  self::$_foo = '42';
  }
}
FirstClass::StaticInit();


class SecondClass extends ParentClass {
  public static function StaticInit()
  {
  self::$_foo = '43';
  }
}
SecondClass::StaticInit();


class ThirdClass extends ParentClass {
  public static function StaticInit()
  {
  self::$_foo = '44';
  }
}
ThirdClass::StaticInit();

FirstClass::Test();
SecondClass::Test();
ThirdClass::Test();
?>

Expected

42, 43, 44

Result

44, 44, 44

Problem is, that I want to have this Fields protected. I can not do an abstract static method, because of Strict Error.

Is It Posibble?

Regards.

Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111

0 Answers0