The WebAbility® Network Developers - Documentation
WebAbility Site
\patterns\Factory reference

I. Structure:

- namespace:

- Direct superclass:

- Direct known subclasses:

II. Description:

This is a pattern used to build "Products" inside the "Factory".
The class represents the factory, and have the facilities to build the products which are instances of any defined classes in the factory.

First of all you have to create an instance of the factory, then give to the factory a catalog of available products,
and finally call the builder of a product in the catalog.

A factory cannot build a product that is not in its catalog.

The classes of the product should be loaded or at least easily loadable.


// creates the factory
class myFactory extends \patterns\Factory
function __construct()
parent::__construct(array('i' => 'Integer', 'r' => 'Real', 'c' => 'Complex'));

// Integer, Real and Complex are already created classes.

// gets the factory instance
$F = new myFactory();

// create some products
$I = $F->newProduct('i', 4.6);
$R = $F->newProduct('r', 8.6);
$C = $F->newProduct('c', 12.6, -4);

... Use your products ...

III. Reference:

3.1. Contructor:

Creates the instance of your factory with the pre-defined catalog.

3.2. Constants:

There is no constant into this class.

3.3. Attributes:

There is no attributes into this class.

3.4. Methods:

public newProduct($productid, [$param1, ...])
This method is used to create a product based on its id. The parameters will be passed to the constructor of the product.
If the product does not exist, a null is returned.

public registerProduct($productid, $productclass)
This method is used to add a new product definition in the catalog.

public unregisterProduct($productid, $productclass)
This method is used to remove a product definition from the catalog.

protected classLoader($class)
This method is called only if the class has not be found, before creating it, in sight to load it.
It may replace the PHP __autoload() function.