Purpose of the Collection Class

Purpose of the Collection Class

Applications frequently have objects that contain a group of other objects. For example, if you were building an application for a university's registrar's office, you would need to have a Student class and a Course class. A Student object would probably have more than one Course object associated with it. The most obvious way to construct this would be to have an array of Course objects as a member variable of the Student object.

   /* student class */    class Student {      public $courses = array();      // ... other methods/properties    }    /* using the student class */    $objStudent = new Student(1234); //constructor not shown above    foreach($objStudent->courses as $objCourse) {      print $objCourse->name;    } 

Of course, if the most obvious approach were the best possible approach, we wouldn't have needed to write this chapter.

The above method has a few problems. First, exposing the array of Course objects as a public member variable breaks encapsulation. There is no opportunity to validate changes to the array or change the state of the Student object, should that be necessary. Second, it's not obvious from this implementation how the courses will be indexed in the array and how to traverse that array to find a specific Course object in which you might be interested. Third, and most important, to ensure that the courses array is available to any code that might want to use the Student object, you need to pull up all the course information from the database at the same time as the student information. This means that even if you want to print only the student's name, you have to fetch all the information about courses, unnecessarily increasing load on the database server and bogging down the entire application.

The Collection class is designed to solve all these problems. It provides an OO wrapper around an array and implements a mechanism that allows lazy instantiation. Lazy instantiation defers creation of the members of the collection until such time as they are actually needed. It's called "lazy'' because the application will determine on its own when to instantiate the objects. When you create a Student object, the Course objects should not be created at the same time. Only when you first attempt to access the courses should they be created, and this should happen automagically without requiring you to do anything special.

The functional requirements of the Collection class are as follows:

  1. Establish a wrapper around an array of objects.

  2. Provide methods to add, remove, and fetch the member objects in a predictable, obvious way.

  3. Allow you to easily determine how many member objects exist in the collection.

  4. Enable you to use lazy instantiation to save system resources.

Professional PHP5 (Programmer to Programmer Series)
Professional PHP5 (Programmer to Programmer Series)
Year: 2003
Pages: 182

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net