alpine Posted November 5, 2006 Share Posted November 5, 2006 This is my first dive into writing a class (i hope this is considered a class *lol*), and i'm not trying to re-invent the wheel here - just figured a select list would be an overcoming thing to start fiddeling with to get a class up and running.I don't know if this is done in fairly good practice or wwwway off - it works though. Kill me with improvements/changes/possible problems on such class (green on when to use var,private,public etc...)[code]<?phpclass select{var $options = array();function selected($value=""){$this->selected = $value;}function add($key,$value=false){if($value == false) $value = ucfirst($key);$this->options[$key] = $value;}function name($name=false){if($name==false) $this->name = ""; else $this->name = " name=\"$name\"";}function id($id=false){if($id==false) $this->id = ""; else $this->id = " id=\"$id\"";}function write(){$liste = "";foreach($this->options as $key => $value){if($key==$this->selected) $selected = " selected=\"selected\""; else $selected = "";$liste .= "<option value=\"".$key."\"".$selected.">".$value."</option>\r\n";}$head = "<select".$this->name.$this->id.">\r\n";$foot = "</select>\r\n";echo $head.$liste.$foot;}}$select = new select();$select->selected("1");$select->name("my_select_name");$select->id("my_select_id");$select->add("choose");$select->add("1","Email");$select->add("2","Phone");$select->add("3","Letter");$select->write();?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/26221-basics-ok-or-not-in-this-class/ Share on other sites More sharing options...
448191 Posted November 5, 2006 Share Posted November 5, 2006 Please consider this alternative:[code]<?phpclass Select{ var $options = array(); var $id; var $selected; var $name; function add($options){ foreach($options as $key=>$text){ if(empty($text)){ $text = ucfirst($key); } $this->options[$key] = $text; } } function render(){ foreach($this->options as $key => $text){ $selected = $key == $this->selected ? " selected=\"selected\"": ''; $optionHtmlArr[] = "\t<option value=\"".$key."\"".$selected.">".$text."</option>"; } if(count($optionHtmlArr)){ echo "<select id ='".$this->id."' name='".$this->name."'>\n".implode("\n",$optionHtmlArr)."\n</select>"; } }}$select = &new Select();$select->selected = "1";$select->name = "my_select_name";$select->id = "my_select_id";$select->add(array('choose'=>'','1'=>'Email','2'=>'Phone','3'=>'Letter'));$select->render();?>[/code]Mayor differences:1) It properly initializes properties.2) It doesn't use methods to assign properties that should be publicly available.3) It seperates business and presentation logic (no html except in "render"). Quote Link to comment https://forums.phpfreaks.com/topic/26221-basics-ok-or-not-in-this-class/#findComment-119928 Share on other sites More sharing options...
448191 Posted November 5, 2006 Share Posted November 5, 2006 LOL, i thought you were on php4..I missed this comment: [quote]green on when to use var,private,public etc..[/quote]And it's also in your sig. Sorry, modification:[code]<?phpclass Select{ private $options = array(); public $id; public $selected; public $name; public function add($options){ foreach($options as $key=>$text){ if(empty($text)){ $text = ucfirst($key); } $this->options[$key] = $text; } } public function render(){ foreach($this->options as $key => $text){ $selected = $key == $this->selected ? " selected=\"selected\"": ''; $optionHtmlArr[] = "\t<option value=\"".$key."\"".$selected.">".$text."</option>"; } if(count($optionHtmlArr)){ echo "<select id ='".$this->id."' name='".$this->name."'>\n".implode("\n",$optionHtmlArr)."\n</select>"; } }}$select = new Select();$select->selected = "1";$select->name = "my_select_name";$select->id = "my_select_id";$select->add(array('choose'=>'','1'=>'Email','2'=>'Phone','3'=>'Letter'));$select->render();?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/26221-basics-ok-or-not-in-this-class/#findComment-119929 Share on other sites More sharing options...
448191 Posted November 5, 2006 Share Posted November 5, 2006 Btw, "var" is depreciated.Look in [url=http://nl3.php.net/manual/en/language.oop5.visibility.php]the manual[/url] for practical info on (property) visablilty.Aslo consider turning on E_STRICT error reporting. Quote Link to comment https://forums.phpfreaks.com/topic/26221-basics-ok-or-not-in-this-class/#findComment-119930 Share on other sites More sharing options...
alpine Posted November 5, 2006 Author Share Posted November 5, 2006 That's no prob - I find myself overlooking bits and pieces in threads all the time...Thanks a lot, i still need to climb some miles in these fields ::) Quote Link to comment https://forums.phpfreaks.com/topic/26221-basics-ok-or-not-in-this-class/#findComment-119931 Share on other sites More sharing options...
Jenk Posted November 6, 2006 Share Posted November 6, 2006 I'd separate the html rendering from the object, into an object of it's own, but otherwise it appears ok.I'd also add a bit of polymorphism to the add method, so you can either add many with an array, or a single element as you had previously. Quote Link to comment https://forums.phpfreaks.com/topic/26221-basics-ok-or-not-in-this-class/#findComment-120298 Share on other sites More sharing options...
alpine Posted November 6, 2006 Author Share Posted November 6, 2006 ThanksAs a note to the original post: The "handle"-php and ways of doing it - that was not my issue here, simply how to make a class the way it should be done. What's in it (different functions and the php rendering) can be somewhat different and based on individual needs and prefs - here it was provided just to fire it up.Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/26221-basics-ok-or-not-in-this-class/#findComment-120303 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.