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

Interface Methods

public string|array getCallback ( ) [line 138]

Returns the callback function/method


[ Top ]
public array getParams ( ) [line 144]

Returns the parameters to pass to the callback


[ Top ]
public string getType ( ) [line 132]

Returns the Collection Class|Interface Type


[ Top ]
public bool isLoaded ( ) [line 150]

Returns if the Collection has been loaded


[ Top ]
public void setCallback ( string $name, [string|StdClass $obj] ) [line 117]

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:

  1. $coll->setCallback ("function_name");

To set a class/object method (3 methods):

  1.  $col->setCallback ("method_name""class_name");
  2.  // 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 ]
public void setType ( string $type ) [line 123]

Sets the type of Class|Interface the Collection will accept

Parameter(s):

  • (string) $type

[ Top ]