note 91807 deleted from language.oop5 by danbrown
| From: | danbrown@php.net | Date: | Fri, 26 Jun 2009 13:01:24 +0000 |
| Subject: | note 91807 deleted from language.oop5 by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-157528@lists.php.net to get a copy of this message | ||
Note Submitter: aaron dot mcgowan at cybergeeksoftware dot com
----
Since PHP does not support static classes, you can though however mock this behavior. There are a
couple of things that can be done, but here is an article I recently posted on my blog -
www.cybergeekweb.com.
How is everyone today? Anywho, today with this post I am going to discuss PHPâs Object Oriented
Programming & Design techniques for Static Classes.
Well I am sure that if you have came across this post and you have read this far, you are looking to
figure out how to create static classes in PHP. Well guess what? You can not! However, PHP does
provide the necessary class method modifiers to mock this type of behaviour.
Unlike other languages like Java, with PHP we have to do a little bit more work to accomplish the
same effect. Okay, so with this article I am assuming the readers know how to create classes and
terms in object oriented design & programming.
Steps for creating a class in PHP with static behaviour:
First we want to create an empty class definition.
class MyStaticClass {
}
Secondly, we do not want to instantiate this class ever! So, what we want to do is create the
constructor and destructor for this class with private visibility modifiers. We also want to include
the final keyword on this method declarations. Reason being, we should never be able to instantiate
this class and or derived classes should never be able to be instantiated. So, our class should now
look something like this:
class MyStaticClass {
final private function __construct() { /* void */ }
final private function __destruct() { /* void */ }
}
And now, we have our first Static class definition in PHP. Now, what we want to do is make sure that
all other methods and member variables within this class all have static modifiers.
We also want to remember that any derived classes do not need to have a constructor or destruct
method definition. Reason being is that the final keyword used in our static classâs
constructor and destructor method declaration simply means that this is the only method that can be
the same signature within any derived classes.
Anyways, I must get back to work⦠:P Hope you all have a great day and if you have any
questions or concerns please feel free to comment! :D