Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A factory that remembers
<?php
/**
* @property int $id
* @property string $class_name
*/
abstract class root {
/**
* the identifier of this object
* @var int
*/
protected $id = null;
/**
*
* @var array
*/
protected $data = array();
/**
*
* @param int $id
*/
public function __construct( $id ) {
$this->data['id'] =
$this->id =
$id;
}
/**
* Set a value for this object
* @param stirng $var
* @param mixed $value
*/
public function __set($var, $value) {
$var = strtolower($var);
if(in_array($var, array('id', 'class_name'))) {
throw new Exception('You can\'t set that!');
}
$this->data[$var] = $value;
}
/**
* Get a value from this object
* @param string $var
* @return mixed
*/
public function __get($var) {
$var = strtolower($var);
switch($var){
case 'class_name':
return $this->__toString();
break;
default:
return isset($this->data[$var])? $this->data[$var]:null;
break;
}
}
public function __toString(){
return get_class($this);
}
}
/**
* @property string $name
*/
class User extends root {
public function __construct( $id ) {
parent::__construct($id);
}
}
<?php
class factory {
/**
* Hold all of the items in the factory
* @var array
*/
protected static $created = array();
/**
* Create/Load an object from the factory
* @param string $className
* @param int $id
* @return root
*/
public static function Create($className, $id=null ) {
$className = strtolower($className);
if(!isset(self::$created[$className][$id])) {
self::$created[$className][$id] = new $className($id);
}
return self::$created[$className][$id];
}
/**
* Load an object from the factory
* @param string $className the object's class
* @param int $id the id of the object you are loading
* @return root or null
*/
public static function Load($className, $id ) {
$className = strtolower($className);
return (isset(self::$created[$className][$id]))? self::$created[$className][$id]:null;
}
/**
* Add an object to the factory, has no effect if the object is already part of the factory
* @param string $className
* @param object $class
*/
public static function Add(root &$class) {
$className = strtolower($class->class_name);
if(!isset(self::$created[$className][$class->id])) {
self::$created[$className][$class->id] = &$class;
}
}
/**
* Destroy an instance of something in the factory and the object itself
* @param root $class the class being destroyed
*/
public static function Destroy(root &$class){
$className = strtolower($class->class_name);
$id = $class->id;
unset(self::$created[$className][$id]);
$class = null;
}
}
<?php
require 'dummy_classes.php';
require 'factory.class.php';
/**
* @var User
*/
$u1 = factory::Create( 'User', 1 );
/**
* copy of $u1
* @var User
*/
$u2 = factory::Create( 'User', 1 );
$u1->name = 'Jack';
$u2->name = 'Sally';
/**
* @var User
*/
$u3 = factory::Create( 'User', 2 );
$u3->name = 'Jim';
/**
* @var User
*/
$u4 = new User(3);
$u4->name = 'Doug';
factory::Add($u4);
/**
* copy of $u4
* @var User
*/
$u5 = factory::Load('User', 3);
///should be the same
echo $u1->name . PHP_EOL; // #=>Sally
echo $u2->name . PHP_EOL; // #=>Sally
///should be by itself
echo $u3->name . PHP_EOL; // #=>Jim
///should be the same
echo $u4->name . PHP_EOL; // #=>Doug
echo $u5->name . PHP_EOL; // #=>Doug
factory::Destroy($u3);
var_dump($u3); // #=> null
$u3 = factory::Load('User', 2);
var_dump($u3); // #=> null
@SeanJA
Copy link
Author

SeanJA commented May 21, 2010

An implementation of a factory, includes add, create, destroy, load functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment