Voting

: two minus two?
(Example: nine)

The Note You're Voting On

Anonymous
16 years ago
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

<< Back to user notes page

To Top