Defining only base classes and stack them togeher to get a complete dynamic class object with class inheritance and so on…
In this example its about a social network, defining family and frieds and everyone someone knows as a class element with own name and handle basing on more or less the same class.
<?PHP class Human{ private $name; private $birthday; private $sex; private $mother; private $father; private $siblings = array(); private $partner; private $children = array(); private $hobbies = array(); private $history = array(); public function __construct($name){ $this->name = $name; } public function addChildren($val){$this->children[] = $val;} public function addSiblings($val){$this->siblings[] = $val;} public function addHobbies($val){$this->hobbies[] = $val;} public function addHistory($val){$this->history[] = $val;} public function __desctruct(){} public function __set($var,$val){ $this->$var = $val; } public function getAunts(){ // $this->mother->siblings // $this->father->siblings } public function getCousins(){ // $this->mother->siblings[]->children // $this->father->siblings[]->children } public function getGrandParents(){ // $this->mother->mother // $this->mother->father // $this->father->mother // $this->father->father } } class NoExistentHuman{ private $name; public function __construct($name){ $this->name = $name; } } class Hobby{ private $description; public function __construct($description){ $this->description = $description; } public function __desctruct(){} public function __set($var,$val){ $this->$var = $val; } } class History{ private $description; private $when = array(); private $type; public function __construct($description){ $this->description = $description; } public function __desctruct(){} public function __set($var,$val){ $this->$var = $val; } } class Sex{ public $male = "male"; public $female = "female"; } $humans = array("Mike", "Jess", "Hans", "Susi", "Moni"); $sex = new Sex(); // create humans >:) foreach($humans as $human){ $$human = new Human($human); } // create planned humans 😉 $Aby = new NoExistentHuman("Aby"); // link them together $Mike->birthday = "20.10.1983"; $Mike->sex = $sex->male; $Mike->partner = $Jess; $Mike->mother = $Susi; $Mike->father = $Hans; $Mike->addChildren($Aby); $Mike->addSiblings($Moni); $Mike->addHobbies(new Hobby("Programming")); $Mike->addHistory(new History("Lott Autoteile GmbH")); Debug::dump($Mike, "Mikes Information"); // class from http://codeaid.net/php/improved-var_dump%28%29-with-colored-output ?>and the output or “storage data” looks like this:
Mike's Information object(Human)#2 (10) { ["name":"Human":private]=> string(4) "Mike" ["birthday":"Human":private]=> string(10) "20.10.1983" ["sex":"Human":private]=> string(4) "male" ["mother":"Human":private]=> object(Human)#5 (10) { ["name":"Human":private]=> string(4) "Susi" ["birthday":"Human":private]=> NULL ["sex":"Human":private]=> NULL ["mother":"Human":private]=> NULL ["father":"Human":private]=> NULL ["siblings":"Human":private]=> array(0) { } ["partner":"Human":private]=> NULL ["children":"Human":private]=> array(0) { } ["hobbies":"Human":private]=> array(0) { } ["history":"Human":private]=> array(0) { } } ["father":"Human":private]=> object(Human)#4 (10) { ["name":"Human":private]=> string(4) "Hans" ["birthday":"Human":private]=> NULL ["sex":"Human":private]=> NULL ["mother":"Human":private]=> NULL ["father":"Human":private]=> NULL ["siblings":"Human":private]=> array(0) { } ["partner":"Human":private]=> NULL ["children":"Human":private]=> array(0) { } ["hobbies":"Human":private]=> array(0) { } ["history":"Human":private]=> array(0) { } } ["siblings":"Human":private]=> array(1) { [0]=> object(Human)#6 (10) { ["name":"Human":private]=> string(4) "Moni" ["birthday":"Human":private]=> NULL ["sex":"Human":private]=> NULL ["mother":"Human":private]=> NULL ["father":"Human":private]=> NULL ["siblings":"Human":private]=> array(0) { } ["partner":"Human":private]=> NULL ["children":"Human":private]=> array(0) { } ["hobbies":"Human":private]=> array(0) { } ["history":"Human":private]=> array(0) { } } } ["partner":"Human":private]=> object(Human)#3 (10) { ["name":"Human":private]=> string(4) "Jess" ["birthday":"Human":private]=> NULL ["sex":"Human":private]=> NULL ["mother":"Human":private]=> NULL ["father":"Human":private]=> NULL ["siblings":"Human":private]=> array(0) { } ["partner":"Human":private]=> NULL ["children":"Human":private]=> array(0) { } ["hobbies":"Human":private]=> array(0) { } ["history":"Human":private]=> array(0) { } } ["children":"Human":private]=> array(1) { [0]=> object(NoExistentHuman)#7 (1) { ["name":"NoExistentHuman":private]=> string(3) "Aby" } } ["hobbies":"Human":private]=> array(1) { [0]=> object(Hobby)#8 (1) { ["description":"Hobby":private]=> string(11) "Programming" } } ["history":"Human":private]=> array(1) { [0]=> object(History)#9 (3) { ["description":"History":private]=> string(19) "Lott Autoteile GmbH" ["when":"History":private]=> array(0) { } ["type":"History":private]=> NULL } } }