note 88045 added to language.oop5.magic

From: Date: Wed, 07 Jan 2009 15:54:32 +0000
Subject: note 88045 added to language.oop5.magic
Groups: php.notes 
Request: Send a blank email to php-notes+get-148922@lists.php.net to get a copy of this message
Regarding __toString: <?php class my_tag_A{ public $id=''; public $href=''; public $target=''; public $class=''; public $label=''; function __construct($href, $label){ $this->href = $href; $this->label = $label; } public function __toString(){ return '<a '.$this->nz_arr(array('id', 'href', 'target', 'class')). ' >' . $this->label . '</a>'; } function nz_arr($attrib_arr){ $s = ''; foreach($attrib_arr as $attrib){ $s .= $this->nz($attrib); } return $s; } /** * Print the tag attribute if it is not blank, such as id="$this->id" * @param string $attrib * @return string */ function nz($attrib){ $s = ''; if($this->$attrib != '') $s = $attrib .' = "' . $this->$attrib . '"'; return $s; } //This causes RECURSION because of parsing between double quotes. This is a very UNEXPECTED behaviour! function nz_($attrib){ $s = ''; if($this->$attrib != '') $s = "$attrib = \"$this->$attrib\""; return $s; } }//end class //usage $a = new my_tag_A('abc.php', 'ABC'); $a->target = '_blank'; echo $a; //prints: // <a href="abc.php" target="_blank" >ABC</a> ?> ---- Server IP: 92.48.74.199 Probable Submitter: 82.144.250.86 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.magic.php Edit -- https://master.php.net/note/edit/88045 Del: integrated -- https://master.php.net/note/delete/88045/integrated Del: useless -- https://master.php.net/note/delete/88045/useless Del: bad code -- https://master.php.net/note/delete/88045/bad+code Del: spam -- https://master.php.net/note/delete/88045/spam Del: non-english -- https://master.php.net/note/delete/88045/non-english Del: in docs -- https://master.php.net/note/delete/88045/in+docs Del: other reasons-- https://master.php.net/note/delete/88045 Reject -- https://master.php.net/note/reject/88045 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#148922) next »