views:

710

answers:

4

Lately I've been seeing a lot of talk regarding PHP's lack of late static binding until 5.3.

From what I've read proper implementations of stuff like ActiveRecord are not possible until the language has this feature.

So, I'm curious about:

  • Which languages do support it, specifically those commonly associated with web development such as Python, Ruby, Perl, Java, C#, (JavaScript?).
  • Which actually make use of it on a regular basis?
A: 

I completely misunderstood the what late static binding is. Here's what Wikipedia says.

Late static binding is a variant of [name] binding somewhere between static and dynamic binding. Consider the following PHP example:

class A {
 static $word = "hello";
 static function hello() {print self::$word;}
}

class B extends A {
 static $word = "bye";
}

B::hello();

Without late static binding, the PHP interpreter binds the function hello() to class A when the programmer is clearly expressing the function on class B. The absence of late static binding would cause "hello" to be printed, not "bye" despite hello() being called on class B.

Late static binding in the interpreter means that $word is determined at runtime. In this case it would reference B::$word if B::hello() is called and A::$word if A::hello() is called. This does require a change in keywords from self to static (possibly available beginning with PHP 5.3) in A::hello()

class A {
 static $word = "Welcome";
 static function hello() {print static::$word;}
}

class B extends A {
 static $word = "bye";
}

B::hello();
eed3si9n
I am afraid that DI has nothing to do with static binding ...
Guillaume
I can see how the two terms could be confused though.
troelskn
+3  A: 

If you want a work around, that admittedly is a little time consuming, yet will be easily removed when php 5.3 becomes available and mainstreamed, you can try the following code.

class Specific_Model extends Model{

    public static function GetAll($options = null){

        parent::GetAll($options, get_class());

    }

}


class Model{

    public static function GetAll($options = null, $class = null){

       if(is_null($class)) $class = get_class();  

       /* Do stuff here */

    }

}

Then you can use the following code...

Specific_Model::GetAll($options);

And easily strip out the excess code when moving to php 5.3.

navitronic
A: 

The concept of "Late Static Binding" is a hack to patch up the fact that PHP has statically declared classes. Most dynamic languages have an object system, where a class is an object. PHP on the other hand, keeps code and runtime completely separated (Like C/C++). This has all sorts of weird implications, that we would be better of without.

troelskn
A: 

Without late static binding, the PHP interpreter binds the function hello() to class A when the programmer is clearly expressing the function on class B. The absence of late static binding would cause "hello" to be printed, not "bye" despite hello() being called on class B.