Jump to content

PHP5 - General Object Factory, the Reflection Class.


w3evolutions

Recommended Posts

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.