Skip to content

Instantly share code, notes, and snippets.

@sagormax
Last active December 8, 2020 05:20
Show Gist options
  • Save sagormax/b8089dad356e47698304a2ac56a14355 to your computer and use it in GitHub Desktop.
Save sagormax/b8089dad356e47698304a2ac56a14355 to your computer and use it in GitHub Desktop.
Late static binding
<?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