w3evolutions Posted May 4, 2009 Share Posted May 4, 2009 There are many times in programing where there is a need to have an object that creates other object or maybe there needs to be a set of instruction or data manipulation that needs to occur before an object gets created. This is called an object factory. Factory is a design pattern that controls the creation of other objects from one entry point. (not the formal definition, but mainly what it is used for.) Example: function do_some_work() { // Possibly manipulate some data, or connect to a database and retrieve information... .... } class ObjectFactory { public function create($classname="") { if(!empty($classname)) { // Do some work $work = do_some_work(); return new $classname($work); } } ....... } class Test { public function __construct($args="") { .... } ...... } $factory = new ObjectFactory(); $test = $factory->create("Test"); The issue with this object factory is that it is not generalized. It also will pass $work to ever object that it creates which might not be what is needed. By the nature of this object you can't create both static (Singleton), and instantiatable (using the reserved word new) objects. There are a few ways this can be accomplished. Example: function do_some_work() { // Possibly manipulate some data, or connect to a database and retrieve information... .... } class ObjectFactory { public static function create($classname="") { // Do some work $work = do_some_work(); if(!empty($classname)) { switch($classname) { case "Singleton": return Singleton::getInstance($work); break; case "Instantiable": return new Instantiable($work); break; ....... } } } ............. } class Singleton { public static function getInstance($args="") { .... } .... } class Instantiable { public function __construct($args="") { .... } } $factory = new ObjectFactory(); $test = $factory::create("Instantiable"); The issue with this class is first the ObjectFactory is an instantiable class itself, therefore taking up more memory and coding. Second, every time a new object needs to be created it must be placed into the switch() statement. Third, again it will pass $work to all of the objects it creates (or returns). On the other hand her is an example of a general Factory object. Example: abstract class ObjectFactory { public function create($classname="") { if(!empty($classname)) { $reflection = new ReflectionClass($classname); if($reflection->isInstantiable()) { return new $class_name($args); } else { return call_user_func(array($classname,"getInstance")); } } } ....... } class Test { public function __construct() { return; } public function sayHi($string="") { echo("Hi: ".$string); return; } } class TestSingleton { public static function getInstance() { ..... } public function sayHi($string="") { echo("Hi: ".$string); return; } } // Will print Hi: Have a nice day. // on the screen ObjectFactory::create("Test")->sayHi("Have a nice day."); // Uses the instantiable Object ObjectFactory::create("TestSingleton")->sayHi("again from a singleton."); // Uses the Singleton Object. This example uses PHP5 reflection class. A very handy class to reverse engineering object. It is also an abstract class which means it can not be instantiated. It allows for object chaining and leaves it to the returning class to "do work". by chaining ->sayHi("again from a singleton."); Lastly, it solves both cases of returning a new instance of an object that is instantiatable and the instance of a singleton object, and gives a standardized way of creating other objects. This is a brief overview of an abstract factory object. Quote Link to comment https://forums.phpfreaks.com/topic/156823-php5-general-object-factory-the-reflection-class/ Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 That's great and all, but this is the wrong forum. This is for PHP Help. Quote Link to comment https://forums.phpfreaks.com/topic/156823-php5-general-object-factory-the-reflection-class/#findComment-825984 Share on other sites More sharing options...
Maq Posted May 4, 2009 Share Posted May 4, 2009 That's great and all, but this is the wrong forum. This is for PHP Help. Exactly, w3evolutions do you have a question? I don't see any spam, are you trying to write your own tutorial? Quote Link to comment https://forums.phpfreaks.com/topic/156823-php5-general-object-factory-the-reflection-class/#findComment-826003 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.