SingleObject
rethinking OOP
Code Examples (in php)
SingleObject lets you build complex applications using only 1 object. You don't think that's possible, but it is.
With SingleObject there's no need to instantiate various classes, or use many different methods. You only have ONE, a "generic object" that will adapt itself to the data, and provide you with centralized data-persistence and ORM (object relational mapping) functions.
NOTE: SingleObject is currently compatible with PHP4 only.
Selecting all users of your application...
//we instantiate the object
$user = new generic_object('users');
//clusterize() returns an array of all users
$all_users = $user->clusterize();
...or fetching all meetings!
//we instantiate the object ..and then do the same thing
$meeting = new generic_object('meetings');
$all_meetings = $meeting->clusterize();
Performing an operation on multiple objects
//let's say we already fetched all users...and now we want to delete them all!
if(is_array($all_users)){
for($x = 0; $x < count($all_users); $x++){
$all_users[$x]->delete();
}
}
//or let's say we want to fetch group information for each user
if(is_array($all_users)){
for($x = 0; $x < count($all_users); $x++){
$all_users[$x]->relate('groups'); //see relate() below too
}
}
Fetching a certain user and fetching his groups (relational mapping)
//Suppose we take the USER ID from a web form with method POST
$user->getinput($_POST, 0, -1, 1);
if($user->validateall()){ //if input is good
if($user->fetch()){ //and user exists
$user->relate('groups');
}
}
/*
*
This actually fetches all the group-objects, but these is
*
also the possibility to just list the ID's of the groups
*
that relate to the user, with the function list_ids()
*/
Basic data persistence: easing input-validation, sanitization, and insertion of data in the db.
//we fetch the input for insertion.*/
$user->getinput($_POST, 1, -1, 0);
/*
*
Input is automatically filtered.
*
Only the data belonging to the
*
user gets through.
*
Spurious data is ignored.
*/
$user->insert(1,1,0);
/*
*
With the insertion
* the input is then centrally
*
validated
against the constraints
*
set at database
level:
* - not null
* - type
* - max length
* and then is finally inserted
*/
Integration with the SMARTY TEMPLATE ENGINE: (displaying all groups of your application)
//in the PHP code...
$group = new generic_object('groups');
$groups = $group->clusterize();
$smarty->assign('groups', $groups);
//and in the Template file...
<table width="100%" border="0" cellspacing="0" cellpadding="0">
{foreach from=$groups item=group}
<tr>
<td>{$group->data.groups_groupname}</td>
</tr>
{/foreach}
</table>