PHP
  Home arrow PHP arrow Page 3 - Object Interaction in PHP: Introductio...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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: 4 stars4 stars4 stars4 stars4 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · This second part of the series makes room to implement Composition by writing...
       · Again, great article. Really learn a lot as I'm working through all your articles on...
       · Hello again Matthijs,Very grateful for the compliments. Also, I'm pleased to...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT