Introduction If you’re anything like me, then it’s quite probable that you’ve already included static members and methods in your PHP 5 classes, which means that you should consider yourself a fairly experienced developer. On the other hand, for those who are completely new to the object model introduced with PHP 5, the subject in question is quite simple: essentially, static methods are those that are callable from outside the regular object context (using the :: scope resolution operator), and therefore can to be invoked without needing to use a specific class instance. In addition, properties that have been declared static inside a class should be accessed from outside the object scope, via the “self” keyword, instead of using the $this pseudo variable. However, even though all these concepts may sound a bit intimidating, things are much simpler than you think, since declaring class methods and properties static is achieved by prefixing them with the “static” PHP keyword. That’s all. Of course, as with most features of a specific programming language, using static members and methods needs to be taught by practical examples, which leads directly to the subject of this article. Since in the first tutorial you were provided with a couple of concrete cases where a class used a static property, as well as one static method (remember the definition of the Singleton class), in this tutorial you’ll see another example where these kinds of methods and members are properly utilized. Specifically, I’ll show you how to use static methods to generate disparate outputs from MySQL database tables. If this topic sounds appealing to you, it’s time to get started. Let’s go!
blog comments powered by Disqus |