w3evolutions Posted October 26, 2009 Share Posted October 26, 2009 Issue: Create an object that gives 1 entry point for the creation of all objects, and act's as the API for all object calls. Problem: All (NON-Singleton) objects that need a __construct method must have ONLY optional parameters. Ex: class MySQL { private $a,$b,$c; public function __construct($a,$b,$c) { $this->a=$a; $this->b=$b; $this->c=$c; } public function reportVars() { echo("Vars: ".$a."<br />".$b."<br />".$c."<br />"); } } $args = array("This is a","This is b","This is c"); Factory::_("MySQL",$args)->reportVars(); //This call will fail because the parameters of MySQL->__constuct() are NOT optional Where the factory not only creates the object "MySQL" but it also will pass a given set of parameters to MySQL's constructor. This can almost be accomplished with call_user_func_array the only issue is that you have to create the object prior to using call_user_func_array which means the parameter's of MySQL's constructor must be optional. Quote Link to comment https://forums.phpfreaks.com/topic/179047-php-5-factory-method-with-dynamic-parameters/ Share on other sites More sharing options...
JonnoTheDev Posted October 26, 2009 Share Posted October 26, 2009 Construct params are optional class foo { public function __construct($params = array()) { foreach($params as $key => $val) { $this->$key = $val; } } } $x = new foo(array('a' => 1, 'b' => 2)); Quote Link to comment https://forums.phpfreaks.com/topic/179047-php-5-factory-method-with-dynamic-parameters/#findComment-944694 Share on other sites More sharing options...
salathe Posted October 26, 2009 Share Posted October 26, 2009 Perhaps your factory could use Reflection? $ref = new ReflectionClass('Fruits'); $foo = $ref->newInstanceArgs(array('apple', 'banana', 'cherry')); P.S. That essentially calls new Fruits('apple', 'banana', 'cherry'); Quote Link to comment https://forums.phpfreaks.com/topic/179047-php-5-factory-method-with-dynamic-parameters/#findComment-944702 Share on other sites More sharing options...
w3evolutions Posted October 26, 2009 Author Share Posted October 26, 2009 Correct the reflection class will work, then the issue is making it work with singleton objects. From an API point-of-view being able to have 1 entry point for all objects. Quote Link to comment https://forums.phpfreaks.com/topic/179047-php-5-factory-method-with-dynamic-parameters/#findComment-944704 Share on other sites More sharing options...
JonnoTheDev Posted October 26, 2009 Share Posted October 26, 2009 Correct the reflection class will work, then the issue is making it work with singleton objects I cannot see an issue Quote Link to comment https://forums.phpfreaks.com/topic/179047-php-5-factory-method-with-dynamic-parameters/#findComment-944706 Share on other sites More sharing options...
w3evolutions Posted October 26, 2009 Author Share Posted October 26, 2009 class MySQL_Instantiable { public function __construct($user, $pass, $location, $db) { // do some work } } class MySQL_Singleton { private static $instance; private function __construct() {} public static function getInstance($user, $pass, $location, $db) { //make a singleton } } The issue is the way these 2 objects are created. Respectively: $object = new MySQL_Instantiable(); $singleton = MySQL_Singleton::getInstance(); They are created two totaly different ways. Quote Link to comment https://forums.phpfreaks.com/topic/179047-php-5-factory-method-with-dynamic-parameters/#findComment-944715 Share on other sites More sharing options...
w3evolutions Posted October 26, 2009 Author Share Posted October 26, 2009 Also the end result being: $args = array("User","Password","Localhost","db1"); Factory::_("ObjectName",$params)->query("SELECT * FROM table")->fetchRows(); // or Factory::_("ObjectName",$params)->query("SELECT * FROM table"); Factory::_("ObjectName")->fetchRows(); The factory object will interface directly with the registry so there will be a global placeholder for the object you are creating. Unless it is a singleton therefore does not need the be stored in the registry. The request -> response for that call would be as follows: 1.) Factory checks to see if "ObjectName" exists already 1a.) if so return it as is 1b.) if not call it's __construct or getInstance() 2.) Pass $args to the method in the correct order 3.) return the current instance of the object your working with 4.) call the remaining chained methods Quote Link to comment https://forums.phpfreaks.com/topic/179047-php-5-factory-method-with-dynamic-parameters/#findComment-944728 Share on other sites More sharing options...
salathe Posted October 26, 2009 Share Posted October 26, 2009 Are you having trouble with any particular part? I'm not seeing where you want us to help you out. Quote Link to comment https://forums.phpfreaks.com/topic/179047-php-5-factory-method-with-dynamic-parameters/#findComment-944729 Share on other sites More sharing options...
JonnoTheDev Posted October 26, 2009 Share Posted October 26, 2009 Again, no matter how the object is created either instantiated via new or from a static method returning an object I cannot see where your issue lies with using params required (or optional) in the construct. Quote Link to comment https://forums.phpfreaks.com/topic/179047-php-5-factory-method-with-dynamic-parameters/#findComment-944730 Share on other sites More sharing options...
w3evolutions Posted October 26, 2009 Author Share Posted October 26, 2009 The factory needs to be able to handle the creation of both types of objects and needs to be able to handle method calls during creation that have required parameters. The reflection class will work for objects that can be created via the "new" keyword, but will not work for singleton objects, therefore it will not work in passing required parameters to a singleton object. Quote Link to comment https://forums.phpfreaks.com/topic/179047-php-5-factory-method-with-dynamic-parameters/#findComment-944738 Share on other sites More sharing options...
JonnoTheDev Posted October 26, 2009 Share Posted October 26, 2009 The singleton pattern should return an object! Your objective is confusing. Take the following: <?php class base { public $params; function set($key, $val) { $this->params[$key] = $val; } function get($key) { return $this->params[$key]; } function createParams($array = array()) { foreach($array as $key => $val) { $this->set($key, $val); } } } class foo extends base { public function __construct($params) { $this->createParams($params); } } class bar extends base { public function __construct($params = array()) { $this->createParams($params); } } class factory { public static function getInstance($object, $params = array()) { return new $object($params); } } $x = factory::getInstance('foo', array(1,2,3)); print $x->get(0); ?> Quote Link to comment https://forums.phpfreaks.com/topic/179047-php-5-factory-method-with-dynamic-parameters/#findComment-944760 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.