note 87008 added to language.oop5.overloading

From: Date: Fri, 14 Nov 2008 15:09:53 +0000
Subject: note 87008 added to language.oop5.overloading
Groups: php.notes 
Request: Send a blank email to php-notes+get-146529@lists.php.net to get a copy of this message
<?php /* Here folows a little improvement of the 'strafvollzugsbeamter at gmx dot de' code, allowing each node to hold both 'parameters' and 'child nodes', and differentiate $s->A->B->C ('FOO') from $s->A (same 'FOO', but shouldn't exist) and from $s-A->B (idem). This allows the class, using the interesting suggested syntax ($root->dad->child->attribute, in which 'dad's and 'child's names are dynamically generated), to do something actually useful, like implementing a n-tree data structure (a menu, for instance). It was tested under PHP 5.2.6 / Windows. I know that must there be something better which already do this (probably in the DOM Model classes, or something like), but it was fun to develop this one, for the sake of studying the "magic" methods. Its a compressed version of the code (no comments, too short variable names, almost no identation). I had to compress it in order to add the note. If anyone cares about the full version, just email me. []s erick2711 at gmail dot com */ class nodoMenu{ protected $p = array(); protected $c = array(); public function __construct($t = '', $uri = '', $css = null, $n = 0, $i=0){ $this->p['t'] = $t;$this->p['uri'] = $uri;$this->p['css'] = $css;$this->p['n'] = $n;$this->p['i'] = $i;$this->p['q'] = 0;return $this; } public function add($cn, $ct = '', $cl = '', $css = null){ $nc = new nodoMenu($ct, $cl, $css, $this->p['n'] + 1, $this->p['q']);$this->c[$cn] = $nc;$this->p['q'] += 1;return $this->c[$cn]; } private function isParameter($pn){ return array_key_exists($pn, $this->p); } public function __isset($pn){ if ($this->isParameter($pn)){return(!is_null($this->p[$pn]));} else{return(array_key_exists($pn, $this->c));} } public function remove($cn){ if (array_key_exists($cn, $this->c)){$this->p['q'] -= 1;unset($this->c[$cn]);} } public function __unset($pn){ if ($this->isParameter($pn)){$this->p[$pn] = null;} else{$this->remove($pn);} } public function __set($pn, $v){ $r = null; if ($this->isParameter($pn)){$this->p[$pn] = $v;$r = $v;} else{if (array_key_exists($pn, $this->c)){$this->c[$pn] = $v;$r = $this->c[$pn];} else{$r = $this->add($pn);}} return $r; } public function __get($pn){ $v = null; if ($this->isParameter($pn)){$v = $this->p[$pn];} else{if (array_key_exists($pn, $this->c)){$v = $this->c[$pn];} else{$v = $this->add($pn);}} return $v; } public function hasChilds(){ return(isset($this->c[0])); } public function child($i){ return $this->c[$i]; } public function strPrint($bm = ''){ //Just to show, replace with something better. $m = '';$r = '';$n = $this->p['n']; if ($n > 0){switch($n){case 0:case 1: $qs = 0; break;case 2: $qs = 2; break;case 3: $qs = 6; break;case 4: $qs = 12; break;case 5: $qs = 20; break;case 6: $qs = 30; break;case 7: $qs = 42; break;case 8: $qs = 56; break;} $tab = str_repeat('&nbsp;', $qs);$r .= $tab; if ($bm <> ''){$m = $bm.'.';} $im = $this->p['i'] + 1;$m .= $im;$r .= $m.' ';$r .= $this->p['t']."<br>\n"; } foreach ($this->c as $child){$r .= $child->strPrint($m);} return $r; } public function __toString(){ return $this->strPrint(); } } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 201.48.129.50 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.overloading.php Edit -- https://master.php.net/note/edit/87008 Del: integrated -- https://master.php.net/note/delete/87008/integrated Del: useless -- https://master.php.net/note/delete/87008/useless Del: bad code -- https://master.php.net/note/delete/87008/bad+code Del: spam -- https://master.php.net/note/delete/87008/spam Del: non-english -- https://master.php.net/note/delete/87008/non-english Del: in docs -- https://master.php.net/note/delete/87008/in+docs Del: other reasons-- https://master.php.net/note/delete/87008 Reject -- https://master.php.net/note/reject/87008 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#146529) next »