As I said right at the beginning of this series, static methods let you perform specific tasks, without having to be restricted by the existence of a concrete class instance. In this particular case, I’m going to apply that concept to define a factory class, which will be responsible for fabricating result set processor objects. Having said that, here is the corresponding definition for the entirely new “ResultProcessorFactory” class:
Undoubtedly, the class coded above won’t blow your mind away, since its signature is very simple. Basically, all that this class does is take the name of the result processor object being created and return this object to client code. Of course, this process is carried out by calling the long-awaited “createResultProcessor()” method, which has been defined as static. In addition, it should be noticed that the method also accepts an object of type “Result,” which will be required further to create the appropriate result processor object. Now that you know how the previous class looks, I’d like to show you a quick example that illustrates the complete process for generating different outputs from a specific MySQL result set, as well as for invoking the static “createResultProcessor()” method. To begin with, suppose that you have a simple CUSTOMERS database table that has been populated with the following data: 1 Customer 1 customer1@domain.com 2 Customer 2 customer2@domain.com After filling in the sample database table with some basic information about hypothetical customers, please examine the script below, which uses the static “createResultProcessor()” method to generate different views from the respective database table: try{ // connect to MySQL $db=new MySQL(array $result=$db->query('SELECT * FROM customers'); // create 'ResultToString' processor object //$resultString=ResultProcessorFactory::createResultProcessor header('Content-Type: text/plain'); echo $resultString->fetch(); /* displays the following string of data
[id]=1
[name]=Customer 1 [email]=customer1@domain.com
--------------------------
[id]=2
[name]=Customer 2 [email]=customer2@domain.com
--------------------------
[id]=3
[name]= Customer 3 [email]=customer3@domain.com
--------------------------
[id]=4
[name]= Customer 4 [email]=customer4@domain.com
--------------------------
[id]=5
[name]= Customer 5 [email]=customer5@domain.com
--------------------------
*/ } catch(Exception $e){ echo $e->getMessage(); exit(); } As you can see, the first example uses the static “createResultProcessor()·" method to display the string of data show above. Again, I’d like to stress that no class instance has been created, since the method has been called from outside the object context. Once you understand the previous example, take a look at the following one, which generates some XML nodes from the same result set:
try{
//connect to MySQL
$db=new MySQL(array('host'=>'host','user'=>'user',
Finally, here is the last example, which returns the same MySQL result set as an associative array:
try{
// connect to MySQL
$db=new MySQL(array('host'=>'host','user'=>'user',
Final thoughts In this two-part series, I walked you through the basics of using static members and methods with PHP 5. As you hopefully learned by the hands-on examples, if you need to call specific methods without having to deal with instances of a class, then static methods might be an option that you should take into account during the development of an application. As usual, see you in the next PHP tutorial!
blog comments powered by Disqus |
|
|
|
|
|
|
|