On Mar 1, 2006, at 9:37 AM, Dmitry Stogov wrote:
1) I would very like to see some real example where "static" is
necessary?
Some use cases for late static binding, tested using self on 5.1.2:
1. What class am I really?
class A {
static function myclass() {
return get_class();
}
}
class B extends A {}
echo A::myclass(); // A
echo B::myclass(); // also A
2. Template method pattern for static methods requires late binding:
class Common {
static function doit() {
self::header();
echo "The answer is 42";
self::footer();
}
// could be abstract
static function header() {}
static function footer() {}
}
class Html extends Common {
static function header() {
echo "<html>\n<body><B>";
}
static function footer() {
echo "</B></body>\n</html>";
}
}
echo html::doit(); // Sorry, no html
3. Accessing a static property with late binding:
class A {
static protected $statprop = 'A';
static function getStatprop() {
return self::$statprop;
}
}
class B extends A {
static protected $statprop = 'B';
}
echo A::getStatprop(); // A
echo B::getStatprop(); // Also A
4. Static properties are hard to get at for anything other than the
explicit class name:
class A {
static public $iprop = 'A';
}
$class = 'A';
echo $class::$iprop; // syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM
5. A (non-working) singleton base class implementation combining
multiple late binding issues:
class Singleton {
public $iprop = NULL;
static protected $instance;
static public function getInstance() {
if (!isset(self::$instance)) {
$class = get_class();
$obj = new $class();
self::configure($obj);
self::$instance = $obj;
}
return self::$instance;
}
static protected function configure($obj) {
}
}
class A extends Singleton {
static protected $instance;
static protected function configure($obj) {
$obj = 'A';
}
}
var_dump(A::getInstance()->iprop); //NULL
This is an issue I would very much like to see resolved prior to 6.
Regards,
Jeff
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php