note 106733 added to language.oop5.late-static-bindings

From: Date: Fri, 02 Dec 2011 14:39:31 +0000
Subject: note 106733 added to language.oop5.late-static-bindings
Groups: php.notes 
Request: Send a blank email to php-notes+get-184855@lists.php.net to get a copy of this message
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.. ---- Server IP: 69.147.83.197 Probable Submitter: 77.167.41.139 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.late-static-bindings.php Edit -- https://master.php.net/note/edit/106733 Del: integrated -- https://master.php.net/note/delete/106733/integrated Del: useless -- https://master.php.net/note/delete/106733/useless Del: bad code -- https://master.php.net/note/delete/106733/bad+code Del: spam -- https://master.php.net/note/delete/106733/spam Del: non-english -- https://master.php.net/note/delete/106733/non-english Del: in docs -- https://master.php.net/note/delete/106733/in+docs Del: other reasons-- https://master.php.net/note/delete/106733 Reject -- https://master.php.net/note/reject/106733 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#184855) next »