PHP 5 adds some new features that make using the singleton pattern much easier. The problem was that PHP4 did not have static class variables. So you had to use a global function to act as the singleton creator. However PHP5 removed this problem and made singleton creation something that can be completely inside the class.
The following code show how to create a simple singleton: ( Collapse ) The output of this code will be:
$foo: Setting the example variable
$bar: Setting the example variable
They are identical
This shows that both $foo and $bar are references to the same object.
Using this method of creating objects is useful for situations like database classes or a file i/o class. That if you create more then one instance it is not helpful and just wastes resources.
However using this method does allow for creating more then one instance if it is deemed necessary with out modification of the class. An example of how this might be useful is if you need to connect to two different databases at the same time.
To create a separate instance just create an object of the singleton class normally with the 'new' operator.
For example after the previous code you could add: ( Collapse ) Will output:
$foobar: This is a differance string
$foo: Setting the example variable
As you can see $foobar is a new instance of the singleton class.
To create singletons in PHP4 you had to use a global function that did basicaly what the singleton::create() function does. Since in php4 only global functions could have static variables that was the only way to implement the singleton pattern.
I have just recently created a community for o9softwares development.
Anyone can become a member and post on the community, if you have
questions regarding our work, our products, or just plain programming
questions please feel free to post them.
With the release of PHP5 I have been converting a lot of the code in the o9soft classlibs to php5, the changes mostly are in the OOP area. PHP5 adds full support for constructors and destructors which makes for much more powerful classlibs.
One very good example of this is in our mySQL library, now having support for a destructor we can remove the need for a call to mysql::close() at the end of a script using the mysql lib. now upon the completion of the script the mysql::__destruct() method calls mysql::close() automatically.
As you can see this is a very simple method, all it does is make a call to the debugger, and the close() method. In php4 having the automatic calling of a function was much more cumbersome.
Another very cool new feature in php5 is the ability to create abstract classes, if you do not know what that means... basically it means that a class defined as abstract can not have an object initiated directly, it must be extended. This feature has given us the ability to create a master abstract class that all of our classes are build from that gives all of the classes references to $_err, $_debug, $_db, and $_var. This removes the need for any global statements in any class methods, or having to setup the references in each class definition. Ok to be completely honest this feature wasn't really necessary to implement this master class idea, i just haven't thought of it till now. Previously i could have setup a master class that was not abstract and used that as the master. Having the abstract class removes any problems that might occur if somehow an instance of the master class was created.
Here is a scaled down version of our master class: