Scroll page back to the top

Late Static Bindings in PHP 5.3

Today we will learn about the feature introduced in PHP 5.3 called Late Static Bindings.

Late Static Bindings allow us to use the inherited methods from the extended classes to get results based on the parameters stored within the called class.
As impressive as it might sound, there's no better way than show it on the example.

The example below shows the standard approach and what the output would be without the use of Late Static Bindings.

class Core {
	
	public static $_class = __CLASS__;
	
	public static function run() {
		return self::$_class;
	}

}


class Generator extends Core {

	public static $_class = __CLASS__;

}

echo Generator::run();

In the above example we have a class Core with one property and one method called $_class and run(). The property has the current class name assigned to it by using __CLASS__ constant and the method returns the value assigned to this property.

The second - Generator class, which extends Core and inherits all the properties and methods, only has $_class property re-declared in order to get its own class name.

Even thou we have re-declared the $_class property, the output of the above will return the following when calling run() method via Generator class:

Core

Now, let's have a look at the scenario with the use of the Late Static Bindings. We will now call our class property using static rather than self keyword:

class Core {
	
	public static $_class = __CLASS__;
	
	public static function run() {
		return static::$_class;
	}

}


class Generator extends Core {

	public static $_class = __CLASS__;

}

echo Generator::run();

This time the output will return:

Generator

Now let's have a look at a situation with 3 different classes - each extending the previous one like so:

class Core {
	
	public static $_class = __CLASS__;
	
	public static function get() {
		echo static::$_class;
	}
	
}

class Generator extends Core {

	public static $_class = __CLASS__;
	
	public static function run() {
		Core::get();
		parent::get();
		self::get();
	}
    
}
class Out extends Generator {
	
	public static $_class = __CLASS__;
    
}

Out::run();

The output of the above will be:

Core
Out
Out

The reason why the output is returning Out for the parent keyword is because our method is static and it does stop within the calling class - meaning that references to the parent will also resolve within the calling class.

The same applies to the methods - let's consider the following example, which is practically the same as the previous one, the only difference is that the class name is called from within the method rather than using the property.

class Core {

	public static function get() {
		echo static::thisClass();
	}
	
	public static function thisClass() {
		return __CLASS__;
	}
	
}

class Generator extends Core {

	public static function thisClass() {
		return __CLASS__;
	}
	
	public static function run() {
		Core::get();
		parent::get();
		self::get();
	}
    
}
class Out extends Generator {
	
	public static function thisClass() {
		return __CLASS__;
	}
    
}

Out::run();

The output will be the same as before:

Core
Out
Out

Again, by the use of static keywords, when calling the thisClass method, we will get the name of the calling class rather than the class, in which the method was defined.

I hope this gives you a brief overview of the Late Static Bindings and how they can help you while building your web application.

 
 
 
Add a comment
Add Comment