Created
August 25, 2013 07:49
-
-
Save ackintosh/6332540 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 | |
| abstract class Car | |
| { | |
| protected static $price; | |
| public static function getFormattedPrice() | |
| { | |
| return number_format(self::$price); | |
| } | |
| } | |
| class NissanNote extends Car | |
| { | |
| protected static $price = 1400000; | |
| } | |
| echo NissanNote::getFormattedPrice(); | |
| // 0 |
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 | |
| abstract class Car | |
| { | |
| protected static $price; | |
| public static function getFormattedPrice() | |
| { | |
| return number_format(static::$price); | |
| } | |
| } | |
| class NissanNote extends Car | |
| { | |
| protected static $price = 1400000; | |
| } | |
| echo NissanNote::getFormattedPrice(); | |
| // 1,400,000 |
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 | |
| abstract class Car | |
| { | |
| protected static $price; | |
| public static function getFormattedPrice() | |
| { | |
| return number_format(static::$price); | |
| } | |
| public static function getDescription() | |
| { | |
| return 'price is ' . Car::getFormattedPrice(); // 非転送コール | |
| } | |
| } | |
| class NissanNote extends Car | |
| { | |
| protected static $price = 1400000; | |
| } | |
| echo NissanNote::getDescription(); | |
| // price is 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment