note 84405 added to language.oop5.late-static-bindings
| From: | Anonymous at osu1 dot php dot net | Date: | Sat, 12 Jul 2008 19:49:11 +0000 |
| Subject: | note 84405 added to language.oop5.late-static-bindings | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-141438@lists.php.net to get a copy of this message | ||
Trying to recreate an inheritable static part for an object through a singleton pattern.
<?php
/**
* "Inheritable static" for PHP < 5.3
* << Library/Inheritable.php >>
*/
abstract class Inheritable_Static extends Singleton
{
}
abstract class Inheritable
{
public static function getStatic($className)
{
// Use an abstract Singleton
return Singleton::getInstance($className . '_Static') ;
}
public function goStatic()
{
return self::getStatic(get_class($this)) ;
}
}
/**
* Abstract
* << Library/SayIt/Abstract.php >>
*/
abstract class SayIt_Abstract_Static extends Inheritable_Static
{
public $format ;
}
abstract class SayIt_Abstract extends Inheritable
{
protected $_name ;
public function __construct($name)
{
$this->_name = $name ;
}
final public function sayIt()
{
echo sprintf($this->goStatic()->format, $this->_name) . "\n" ;
}
}
/**
* Concrete
* << Library/SayIt/Hello.php >>
*/
class SayIt_Hello_Static extends SayIt_Abstract_Static
{
}
class SayIt_Hello extends SayIt_Abstract
{
public static function getStatic() { return parent::getStatic(__CLASS__) ; }
}
/**
* Test
*/
SayIt_Hello::getStatic()->format = 'Hello %s' ;
$w = new SayIt_Hello('World') ;
$j = new SayIt_Hello('Joe') ;
echo $w->sayIt() ; // Hello World
echo $j->sayIt() ; // Hello Joe
----
Server IP: 87.124.64.96
Probable Submitter: 84.14.8.8
----
Manual Page -- http://www.php.net/manual/en/language.oop5.late-static-bindings.php
Edit -- https://master.php.net/note/edit/84405
Del: integrated -- https://master.php.net/note/delete/84405/integrated
Del: useless -- https://master.php.net/note/delete/84405/useless
Del: bad code -- https://master.php.net/note/delete/84405/bad+code
Del: spam -- https://master.php.net/note/delete/84405/spam
Del: non-english -- https://master.php.net/note/delete/84405/non-english
Del: in docs -- https://master.php.net/note/delete/84405/in+docs
Del: other reasons-- https://master.php.net/note/delete/84405
Reject -- https://master.php.net/note/reject/84405
Search -- https://master.php.net/manage/user-notes.php