note 106733 deleted from language.oop5.late-static-bindings by tularis
| From: | tularis@php.net | Date: | Fri, 02 Dec 2011 20:02:09 +0000 |
| Subject: | note 106733 deleted from language.oop5.late-static-bindings by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-184863@lists.php.net to get a copy of this message | ||
Note Submitter: michiel ed thalent circle nl
Reason: useless
----
I've created this script to have a standardized way of population classes.
- The class you want to created should extend "Abs"
- The class you want to create should have all parameters defined
- The constructor of your class expects the required parameters
- The factory method expects a travesable object or array
- property names are camelCased (eg. propertName)
- Setters (and getters) are also camelCased (eg. setProperyName)
- Throws an InvalidArgumentExpection if a required param is missing
- Needs php 5.3+
<?php
abstract class Abs {
public static function processParams($config) {
$params = array();
foreach($config as $name => $value) {
$params[lcfirst($name)] = $value;
}
$callParams = array();
$reflClass = new \ReflectionClass(get_called_class());
$options = $reflClass->getDefaultProperties();
$reflMethod = new \ReflectionMethod(get_called_class(), '__construct');
foreach( $reflMethod->getParameters() as $param) {
if(!$param->isOptional() && !isset($params[$param->getName()])) {
throw new \InvalidArgumentException('Missing required parameter for class "' .
get_called_class() . '" : "' . $param->getName() . '"');
}
// Add param to list of required parameters and remove from paramlist
$callParams[] = $params[$param->getName()];
unset($params[$param->getName()]);
}
$callerClass = $reflClass->newInstanceArgs($callParams);
foreach($params as $paramName => $paramValue) {
if(array_key_exists($paramName, $options)) {
$callerClass->{'set'.ucfirst($paramName)}($paramValue);
}
}
return $callerClass;
}
public static function factory($config) {
if(!($config instanceof \Traversable) && !is_array($config)) {
throw new \InvalidArgumentException('Config for "' . get_called_class() .
'"::factory() not traversable');
}
return self::processParams($config);
}
}
?>
Example class
<?php
class Example extends Abs {
/**
*
* @var int
*/
protected $id;
/**
*
* @var string
*/
protected $displayName;
/**
* Url to the logo
* @var string
*/
protected $logo;
public function __construct($id, $displayName) {
$this->id = $id;
$this->displayName = $displayName;
}
public function getId() {
return $this->id;
}
public function getDisplayName() {
return $this->displayName;
}
public function setDisplayName($displayName) {
$this->displayName = $displayName;
}
public function getLogo() {
return $this->logo;
}
public function setLogo($logo) {
$this->logo = $logo;
}
}
?>
Id and displayName are required for this object to work. Logo is is optional.
Actually, logo could also be added to the parameters of the contructor ($logo = '') and is
still recognized as optional.
You can now call:
$test = new Example(3, 'My Name');
$test->setLogo('image.gif');
The same would be to do:
$test = Example::factory(array('id'=>3, 'displayName' => 'My
Name', 'logo' => 'image.gif'));
Leaving out a required parameter will throw an exception.
Moving around the order or the elements has no effect.
You can also use an ArrayObject, SimpleXmlElement etc..