Late Static Bindings

The self keyword does not follow the same rules of inheritance. self always resolves to the class in which it is used. This means that if you make a method in a parent class and call it from a child class, self will not reference the child as you might expect.


<?php

class Php{

	public static function framework(){

		echo static::getClass()."<br/>";

	}

	public static function getClass(){

		return __CLASS__;
	}
}

class ChildPhp extends Php{

	public static function getClass(){

		return __CLASS__;
	}
}


$php = new php;
$php->framework();


$childphp = new ChildPhp;
$childphp->framework();

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s