So let’s face it. PHP is one of the most widely used server side scripting languages for the development of web applications – both open source (ex: WordPress, phpBB, Drupal, etc.) and commercially. However, keep in mind that PHP is a dynamic server side scripting language that in my personal opinion is just recently (since PHP version 5.3 release) started to shape into a major competitor with other languages such as C# for web development using object oriented programming. One of the current draw backs in PHP is the use of the keyword static only being allowed to be used in method signatures and member variable (some of you may call these class properties, class variables, and so on). With this being said – we are unable to create a static class without the use of other pre-written libraries and or other classes employing the techniques used to replicate a static class.
With all of the above being said, here I will share my solution in developing static classes and keeping them static. Let’s start off by opening up a new PHP document in your favourite code editor/IDE (personally mine is phpDesigner developed by Michael Pham).
We will start off by declaring our namespace (note: you must have PHP 5.3 or higher installed to use namespaces in PHP) and class signature.
namespace LIBRARYHELPERS
{
class StaticClass
{
}
}
Now, for a class to be static we need to assure the following:
- The class cannot be instantiated both by external code and internal code for the class.
To meet the requirements of what we must assure our static classes must do and or cannot we will add the constructor and deconstructor methods as final and private containing absolutely no code within the method. The reason we do this is so that this class and or derived classes cannot instantiate an instance of this. This can be done and completed with the following code:
namespace LIBRARYHELPERS
{
class StaticClass
{
final private function __construct()
{
/* void */
}
final private function __destruct()
{
/* void */
}
}
}
Keep in mind that the destructor is actually not required because the constructor cannot instantiated by derived classes from internal and or external source code. However, I have added it.
So, now you have created your first “static†class or at least a piece of it. Keep in mind that this class should NOT be used to contain other source code and or methods. The reason for this is simply because within this class you can actually instantiate an instance of this class. The keyword private allows for the constructor to be invoked by methods strictly defined in this class. Also, note that I have declared this class’ constructor as final. The reason for this is simply because derived classes cannot declare the same method (constructor) resulting in this method to never be overridden. Therefore this class should only be the base/parent class for any classes which you wish to declare static.
Remember! Class methods and or member variables should be defined static.
I hope this has provided an understanding on how to implement static classes within PHP and I look forward to hearing comments, suggestions, and or concerns – feel free to comment.
