<?php class Army { public static $strength = 20; public static function getStrength() { //late static binding... if the child that called the method has a the property, use that one //else, go up the class hierarchy and find the static property return static::$strength; } } echo 'Army strength: ' . Army::getStrength() . '<br/>'; class Batallion extends Army { public static $strength = 10; } echo 'Batallion strength: ' . Batallion::getStrength() . "<br/>";
class Army { public static $strength = 20; public static function getStrength() { return self::$strength; } } class Batallion extends Army { public static $strength = 10; } echo 'Army strength: ' . Army::getStrength() . '<br />'; // Even though we have redefined the $strength property this will output the original value of 20 echo 'Batallion strength: ' . Batallion::getStrength() . '<br /><br /><br />'; // ########################################################################################################################### // ############################################# USING LATE STATIC BINDING ################################################### // ########################################################################################################################### class ArmyLate { public static $strength = 20; public static function getStrength() { // This means that if a child class calling the method has a static $strength property, that will override return static::$strength; } } class BatallionLate extends ArmyLate { public static $strength = 10;