Last active
December 8, 2020 05:20
-
-
Save sagormax/b8089dad356e47698304a2ac56a14355 to your computer and use it in GitHub Desktop.
Late static binding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class User | |
{ | |
public static $name = "Undefined"; | |
public static function stateName() | |
{ | |
return self::$name; | |
} | |
/** | |
* Early binding | |
* | |
* @return string | |
*/ | |
public static function show() | |
{ | |
return self::stateName(); | |
} | |
/** | |
* Late static binding | |
* | |
* @return string | |
*/ | |
public static function get() | |
{ | |
return static::stateName(); | |
} | |
} | |
class EarlyBinding extends User { | |
public static function stateName() | |
{ | |
return "This static method should not be called..."; | |
} | |
} | |
class LateStaticBinding extends User { | |
public static function stateName() | |
{ | |
return "Mohammad Bin"; | |
} | |
} | |
echo EarlyBinding::show(); | |
echo "<br />"; | |
echo LateStaticBinding::get(); | |
// << output >> | |
// Undefined | |
// Mohammad Bin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment