Gumbo_Interface_Collection
Gumbo_Interface_List | --Gumbo_Interface_Collection
Collection Interface
A Collection is a system for defining a one-to-many relationship between two different classes. It will load it's contents only when the information is needed. The Collection callback will be responsible for loading the contents.
The primary class (Student) will contain internal information about the object. It will also reference a list of data (Course) through some association. When the primary class is loaded, it will only load the referenced data when the program needs it. This will allow the program to access Student information without loading the Course list a student is enrolled in.
The Collection requires a callback function, which is responsible for loading the contents of the Collection. The callback is called when the details of the Collection is needed. A callback can be a single function or a class method. The callback would need the Collection object it will be populating and any additional arguments needed.
class Course { public static function loadStudentCourses (Collection $col, $student_id) { $col->setType ("Course"); foreach ($course_list as $course) { $col->add ($course); } }
Since objects are passed by reference, there is no need to return any values. The Collection will accept Course objects (defined by the Type). Certain methods inside the Collection will call the callback before returning their results.
The Student class will define a Collection inside the Constructor. This step would require defining the Callback function. The Student class would also provide a method to return the Collection.
class Student { private $_id; // student ID private $_courses; public function __construct () { $this->_courses = new Collection ("loadStudentCourses", "Course", $this->id); ... } public function getCourses () { return $this->_courses; } ... }
When a new Student is created, the Collection object is created, however it is empty. When the program tries to retrieve details from the Collection, the callback function is called, and the Collection is populated.
Author(s): |
Michael Luster <mluster79@yahoo.com> |
License: | New BSD License |
Copyright: | Copyright (c) 2007, iBayou, Michael Luster |
Link: | http://sourceforge.net/projects/phpgumbo |
Version: | 0.0.1 |
Inherited Methods
- Gumbo_Interface_List::add()
- Gumbo_Interface_List::exists()
- Gumbo_Interface_List::get()
- Gumbo_Interface_List::getAll()
- Gumbo_Interface_List::keys()
- Gumbo_Interface_List::remove()
- Gumbo_Interface_List::removeValue()
- Gumbo_Interface_List::reset()
- Gumbo_Interface_List::size()
- Gumbo_Interface_List::__get()
- Gumbo_Interface_List::__isset()
- Gumbo_Interface_List::__set()
- Gumbo_Interface_List::__unset()
Interface Methods
Sets the callback function/method (extra arguments will be sent to callback as is)
The callback function will be called to load the contents of the Collection. The value could be a function name or a class method. A callback function/method should define a single argument, the Collection. When the callback is executed, the Collection will pass itself (by reference) to the function. The callback will set the values of the Collection.
To set a function:
To set a class/object method (3 methods):
- // substitute an Object ($obj) for "class_name" if necessary
Parameter(s):
- (string) $name :: function/method name (array should be (0=>"class",1=>"method"))
- (string|StdClass) $obj :: class name or Object reference
- precondition:
function/method callable
[ Top ]