Skip to content

Instantly share code, notes, and snippets.

@ackintosh
Created August 25, 2013 07:49
Show Gist options
  • Select an option

  • Save ackintosh/6332540 to your computer and use it in GitHub Desktop.

Select an option

Save ackintosh/6332540 to your computer and use it in GitHub Desktop.
Late Static Binding
<?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
<?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
<?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