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

I. Structure:


- namespace:
\patterns\

- Direct superclass:
\core\WAObject

- Direct known subclasses:
\core\WABase

II. Description:


This is the simplest pattern class to extend to create a singleton.
A singleton is a class that can have only one instance through your whole aplication.
When you intend to create a second instance of the class, an error is thrown.

The class has a static method to get or create the unique instance easily without bothering where is the instance:
- getInstance('name_of_class') to get the unique instance of your class.

You should extend all your singletons from the Singleton class.
Be sure you call the constructor of your father class.


Example:



class DatabaseConnector extends \patterns\Singleton
{
function __construct()
{
parent::__construct();
}

function connect()
{
}
}

... Lots of your code ...

$connector = \patterns\Singleton::getInstance('DatabaseConnector');

... Use your singleton ...




III. Reference:


3.1. Contructor:


\patterns\Singleton()
Creates the unique instance.

3.2. Constants:

There is no constant into this class.


3.3. Attributes:

There are no attributes into this class.


3.4. Methods:


public static getInstance($data)
This method is used to get the instance of the asked class.
If the instance does not exist yet, it will be create.
If the class does not exist, an error is thrown.

public static hasInstance($data)
This method returns true if the instance already exists