<pre>
<?php
/*
HOOP (Human Object Oriented Programming)
The HOOP practice is new, very new, but i personally
think HOOP as a new way of modelling the world into
php or whatever programming language or some kind of
new way to do object oriented programming.
HOOP maximize functions within functions to act like
an object.
the best way of HOOP is HOOP will act like the
Globals variable for every instances like human ~
what we remember in our brain.
to debug, just type print_r(HOOP('DBG')); and we can get all
the instances information, this may probably what you can't get
in traditionaly OOP.
see the code and try,
view the example below, to use, just copy function HOOP
and deploy your function like Fruit function!
HOOP may make your code lengthy a bit but you may decide
worth it or not because to make your traditional class & object
secure, they must also reside in the function so that they don't
appear global which can be accessed using $GLOBALS;
HOOP will be more secure if function name can be retrieved
using func_name() inside a function. thus we can build a list like which
function can access to HOOP function and prevent other malicious
function from accessing HOOP function.
any suggesstion please e-mail me at jimsonchang@yahoo.com
some of the abbrev. i use
A = Add
G = Get
GA = Get All
D = Delete
DA = Delete All
DBG = Debug
FD = Function define // prevent function redefine error
*/
function HOOP($param=null){
static $data;
switch($param){
case 'A':
if(func_num_args() == 4){
$lParam = func_get_args();
array_shift($lParam);
$data[$lParam[0]][$lParam[1]][$lParam[2][0]] = $lParam[2][1];
}
break;
case 'G':
if(func_num_args() == 4){
$lParam = func_get_args();
array_shift($lParam);
if(isset($data[$lParam[0]][$lParam[1]][$lParam[2]])){
return $data[$lParam[0]][$lParam[1]][$lParam[2]];
}
}
break;
case 'GA':
if(func_num_args() == 3){
$lParam = func_get_args();
array_shift($lParam);
if(isset($data[$lParam[0]][$lParam[1]])){
return $data[$lParam[0]][$lParam[1]];
}
}
return $data;
break;
case 'D':
if(func_num_args() == 4){
$lParam = func_get_args();
array_shift($lParam);
unset($data[$lParam[0]][$lParam[1]][$lParam[2]]);
}
break;
case 'DA':
if(func_num_args() == 3){
$lParam = func_get_args();
array_shift($lParam);
unset($data[$lParam[0]][$lParam[1]]);
}
break;
case 'DBG':
return $data;
break;
}
}
function Fruit($param=null){
static $funcDefine;
static $funcInst;
$funcName = 'Fruit';
switch($param){
case 'A':
if(func_num_args() == 3){
$lParam = func_get_args();
array_shift($lParam);
HOOP('A', $funcName, $funcInst, $lParam);
}
break;
case 'G':
if(func_num_args() == 2){
$lParam = func_get_args();
return HOOP('G', $funcName, $funcInst, $lParam[1]);
}
break;
case 'GA':
return HOOP('GA', $funcName, $funcInst);
break;
case 'D':
if(func_num_args() == 2){
$lParam = func_get_args();
HOOP('D', $funcName, $funcInst, $lParam[1]);
}
break;
case 'DA':
if(func_num_args() == 1) HOOP('DA', $funcName, $funcInst);
break;
case 'FD':
$funcDefine = true;
break;
default:
func_num_args()==1 ? $funcInst = $param : $funcInst;
if(!$funcDefine){
function FruitProp($propName, $propVal){
Fruit('A', $propName, $propVal);
}
function FruitColor(){
return Fruit('G', 'color');
}
function FruitAll(){
return Fruit('GA');
}
$funcName('FD');
}
}
}
// new instance name = apple
Fruit('apple');
$price['big'] = 5;
$price['small'] = 2;
// adding properties to HOOP brain
Fruit('A', 'weight', '30 grams');
FruitProp('color', 'red');
FruitProp('quality', 'A1');
FruitProp('price', $price);
Fruit('D', 'quality');
Fruit('DA');
Fruit('DA');
// new instance name = orange
Fruit('orange');
FruitProp('color', 'orange');
// set object Fruit to remember instance apple back.
Fruit('apple');
$apple = FruitAll(); // or $apple = Fruit('GA');
echo 'Debug<br>';
print_r(HOOP('DBG'));
?>
</pre>