Objects collection
The objects collection notion, in objects oriented programming, is useful to collect same class objects. That way, the list notion is going to be an "object".
It is then possible to code specific lists functions (search, xHTML tables view, select view, etc. ).
The class which is our point of interest is named WBcollection(). A documentation is available here.
Class definition
The class is defined that way:
abstract class WBcollection extends WBobject {
protected $_result;
// Class code
}- The reserved word class says that this part of code is a class.
- WBcollection defines the name of our objects collection class.
- The reserved word abstract is used to create an abstract class. (see explanations)
- extends WBobject says that WBcollection is a child of WBobject, our generic class. It is useful to use its definitions (see explanations)
Variables definition
Because this class is manipulating database search results, it is necessary to create a variable used to temporarily store the results array. In this case, the WBcollection::_result variable has been created in protected mode, this one should not be addressed from outside of the class. This variable must be called only from an objects collection.
Functions definition
The generic functions of an objects collection can be multiple. However, the following functions have been retained:
- WBcollection::getFromDB() : function used to obtain different rows from a database using search criteria passed in parameters
- WBcollection::getResults() : function used to get the WBcollection::_result variable content from outside of a WBcollection "child class". This function makes impossible to write data in specified variable while allowing a read possibility for debug mode.
- WBcollection::renderHTMLresults() : function used to render an xHTML table which each row is each object of the collection. The render of this table is entirely configurable.
- WBcollection::renderHTMLselect() : function used to render an xHTML select which each option will be an object of the collection. The choice of values and labels are fully customizable.
Class usage
Because the class is an abstract class, it is necessary to generate a child class through the reserved word extends.
Proceed like this:
class fooChilds extends WBcollection {
// Code de la classe
}Our class is then ready for use.
Many variables and functions should be create to fit the needs of the object.
The GEOcities() class can better light you on the concept. This class manages the GEOcity() objects collections.
Usage example
Here is an example of how to use an objects collection:
$aSearchValues = array(
array(
"geoCityName",
"LIKE",
"Lau%"
),
array(
"geoCityCountryID",
"=",
204
),
array(
"geoCityElevationfeet",
">",
"4800"
));
$oCities = new GEOcities();
$oCities->getGEOcities($aSearchValues)->renderHTMLresults();
// Will render an xHTML table which each line will be
// each object resulting of the query (cities in Switzerland beginning
// by "Lau" and which altitude is beyond 4800 feet)With this piece of code, we remark the ease to search / manipulate / render the collections of rows.