Home arrow PHP arrow Page 3 - Object Interaction in PHP: Introduction to Composition, conclusion

The other side of composition: the “Result” class - PHP

In the first article of this series, the concept of composition in object-oriented programming was explained, and illustrated with an example that implemented composition using two simple classes. In this article, we will create a MySQL database wrapping class that uses the concept of composition.

  1. Object Interaction in PHP: Introduction to Composition, conclusion
  2. Composition in a practical sense: building a MySQL wrapping class
  3. The other side of composition: the “Result” class
  4. Assembling the whole picture: putting the classes to work
By: Alejandro Gervasio
Rating: starstarstarstarstar / 16
July 18, 2005

print this article



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){




            // fetch row

            function fetchRow(){

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


            // count rows

            function countRows(){


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


                        return $rows;


            // count affected rows

            function countAffectedRows(){


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


                        return $rows;


            // get ID from last inserted row

            function getInsertID(){


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


                        return $id;


            // seek row

            function seekRow($row=0){


                                   $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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: