Faking Late Static Binding before php 5.3?

You can use object instances rather than classes. If you want a global symbol, you can use a global variable. Since they are rather unwieldy in PHP, one trick is to wrap it in a function.Eg.

: class ClassA { function call() { return $this->inner(); } function inner() { return "Class A"; } } function ClassA() { static $instance; return $instance? $instance : new ClassA(); } class ClassB extends ClassA { function inner() { return "Class B"; } } function ClassB() { static $instance; return $instance? $instance : new ClassB(); } echo "Class A = " .ClassA()->call(); echo "Class B = " .ClassB()->call() But a better idea might be to avoid global symbols altogether; The reason why it works well in Ruby/Rails, is that Ruby doesn't really have static state in the same way that PHP has.

A class can be rebound and added to at runtime, which allows for easy extension of the framework. In PHP, classes are always final, so referring to them in application code, is a very strong degree of coupling.

You can use object instances rather than classes. If you want a global symbol, you can use a global variable. Since they are rather unwieldy in PHP, one trick is to wrap it in a function.Eg.

: class ClassA { function call() { return $this->inner(); } function inner() { return "Class A"; } } function ClassA() { static $instance; return $instance? $instance : new ClassA(); } class ClassB extends ClassA { function inner() { return "Class B"; } } function ClassB() { static $instance; return $instance? $instance : new ClassB(); } echo "Class A = " .ClassA()->call(); echo "Class B = " .ClassB()->call(); But a better idea might be to avoid global symbols altogether; The reason why it works well in Ruby/Rails, is that Ruby doesn't really have static state in the same way that PHP has.

A class can be rebound and added to at runtime, which allows for easy extension of the framework. In PHP, classes are always final, so referring to them in application code, is a very strong degree of coupling.

1 You could also use singletons, wrap the instance code in static function getInstance() and have function classB() { return classB::getInstance(); } – jcinacio Sep 24 '09 at 22:58.

Unfortunately there is no nice way to do it (otherwise PHPers wouldn't cheer so much for that feature). You have to pass class name. PHP call(); X("ClassB")->call(); The X function should look up, create and return instance of a class.

Suck. – Tyson of the Northwest May 20 '09 at 22:24.

If performance is not an issue, you can use debug_backtrace() to find the called class: $bt = debug_backtrace(); return get_class($bt1'object'); php.net/manual/en/function.debug-backtra....

Often, late static binding is needed when a child calls back a method of the parent who, in turn, calls an abstract static method of the child. I was in such a postition, and could not use static:: as my PHP was not version 5.3 (or later). The following accomplished late static binding by way of debug_backtrace().

Abstract class ParentClass { static function parent_method() { $child_class_str = self::get_child_class(); eval("\$r = ". $child_class_str. "::abstract_static();"); return $r; }// CHILD MUST OVERRIDE TO PUT ITSELF INTO TRACE protected abstract static function abstract_static(); // THIS NEEDS LATE STATIC BINDING private static function get_child_class() { $backtrace = debug_backtrace(); $num = count($backtrace); for($i = 0; $i == __CLASS__) return $backtrace$i"class"; } return null; } } class ChildClass extends ParentClass { static function parent_method(){ return parent::parent_method(); } protected static function abstract_static() { return __METHOD__.

"()"; }// From ParentClass } print "The call was: ". ChildClass::parent_method().

Here is a related article: phpgoodness.wordpress.com/2010/07/20/lat....

Since you cant use static:: , or get_called_class(), or __callStatic, you'll have to call the inner() function with some indication with the called class (as mentioned in the other answers). An instance of the called class would be fine. You can add "Pseudo Static" methods to mimick every static method you need to overwrite.

This doubles your code, but by doing this as below, I hope the code is easier to upgrade once php5.3 comes around: just remove all the ps methods and all the functions referencing them (and change "self" to "static" where needed ..) class ClassA{ static function call() { return self::inner(); } static function inner(){ return "Class A"; } public function _ps_call() { return $this->_ps_inner(); } } class ClassB extends ClassA{ public static function getInstance() { return new self(); } public static function call() { return self::getInstance()->_ps_call(); } static function inner() { return "Class B"; } public function _ps_inner() { return self::inner(); } } echo "Class A = " . ClassA::call(); echo "Class B = " . ClassB::call().

.. which becomes even more elaborate if you add static vars to the example. In the getInstance() method, you could copy all get_class_vars to instance vars like $this->_ps_foo, which you should use instead of their static versions inside every ps instance method ... – pike Dec 24 '10 at 22:26.

I ask this because it could help in making new instances of the derived class, from a base class, by calling a derived class's static method instead of having to create a new instance of the derived class - or explicitly defining a 'getClass' method for each derived class. I'm not sure what kind of lexical / whatever-it's-called problems this would make with parsing. I don't think it could really collide with any contexts where you would use static otherwise - variable / method declaration.

Even more so, is there a way to get the class's name to which the keywords 'self', 'parent', or 'static' refer? Echo "Self: ". Echo "Parent: ".

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions