PHP
  Home arrow PHP arrow Page 3 - Object Interaction in PHP: Introduction to Composition, conclusion
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Object Interaction in PHP: Introduction to Composition, conclusion
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 16
    2005-07-18


    Table of Contents:
  • Object Interaction in PHP: Introduction to Composition, conclusion
  • Composition in a practical sense: building a MySQL wrapping class
  • The other side of composition: the “Result” class
  • Assembling the whole picture: putting the classes to work

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Object Interaction in PHP: Introduction to Composition, conclusion - The other side of composition: the “Result” class
    ( Page 3 of 4 )

    We’ve already seen that the method “performQuery()” returns a new instance of a “Result” object. Therefore, there must be a class that defines the blueprints for it. Fortunately, this class is quite simple to read, and its definition is as follows:

    class Result {

                var $mysql; // instance of MySQLConnector object

                var $result; // result set

                function Result(&$mysql,$result){

                            $this->mysql=&$mysql;

                            $this->result=$result;

                }

                // fetch row

                function fetchRow(){

                            return mysql_fetch_array($this->result,MYSQL_ASSOC);

                }

                // count rows

                function countRows(){

                            if(!$rows=mysql_num_rows($this->result)){

                                       $this->mysql->isError('Error counting rows');

                            }

                            return $rows;

                }

                // count affected rows

                function countAffectedRows(){

                            if(!$rows=mysql_affected_rows($this->mysql->conId)){

                                       $this->mysql->isError('Error counting affected rows');

                            }

                            return $rows;

                }

                // get ID from last inserted row

                function getInsertID(){

                            if(!$id=mysql_insert_id($this->mysql->conId)){

                                       $this->mysql->isError('Error getting ID');

                            }

                            return $id;

                }

                // seek row

                function seekRow($row=0){

                            if(!mysql_data_seek($this->result,$row)){

                                       $this->mysql->isError('Error seeking row');

                            }

                }

    }

    Our brand new “Result” class behaves in a simple manner, and its constructor takes two parameters (remember how it was created within the “MySQLConnector” class). The first parameter &$mysql, is a reference of a “MySQLConnector” object, in order to provide database connectivity, and the second, $result is the result set obtained after performing the query. Once passed, they’re assigned as class data members.

    The rest of the methods are pretty easy to follow, as you’ll probably agree. The “fetchRow()” method is used to return a row from the result set, as we’ve probably seen hundreds of times.

    To provide the class with the ability to count rows, I’ve defined the “countRows()” method, which simply retrieves the number of rows returned by the query. This might be useful for a paging class or other system that needs to find out the total number of rows.

    Okay, now hold on your breath a little longer until we’ve seen the rest of the class methods.

    The next remaining methods are “countAffectedRows()”, handy for finding out how many rows were affected by an INSERT, UPDATE or DELETE SQL statement; “getInsertID()”, which retrieves the ID of an AUTO_INCREMENT field, after an insertion operation; and finally “seekRow(), that places the pointer at a specified position within the result set. As you might guess, I’ve added to the class the most common operations associated with MySQL. However, you might add to it your own methods to extend its functionality.

    Notice that each method handles an error condition by calling directly the “isError()’ method, provided by “MySQLConnector” class, like this:

    $this->mysql->isError('Error getting ID');

    As you can appreciate, “isError()” is invoked by all of the methods, providing a centralized mechanism to handle errors. One possible improvement would be writing a method that degrades the application gracefully when an error occurs, instead of displaying ugly messages.

    At this time, we’ve finished defining our “Result” class, so we’re ready to implement the classes. Just keep reading to find out how they’re used in a PHP application.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT