Frenz Posted February 26, 2008 Share Posted February 26, 2008 Learnt OOP PHP basics yesterday, got a few good hints from a tutorial, and now i'm trying to make a navigation sidebar creator by using classes and objects. Question is, am i doing this in a good way, am i thinking right? This is mostly going to be a statistic tryout. Explanations will be great! The echo "test"; in the end of the code is just to see if there is no errors, so disregard it. <?php /* This is going to be a shot at writing a navigation sidebar using a class(sidebar) or several. Using objects to create the menu head and each menu item. Thinking about it, this might be a hard one, since i learnt OOP PHP yesterday... */ class sidebar { var $sidebar_title; var $sidebar_bgcolor; var $sidebar_heigth; var $sidebar_width; var $sidebar_border; function sidebar() { } function set_title($input_title){ } function set_bgcolor($input_bgcolor){ } function set_height($input_height){ } function set_width($input_width){ } function set_border($input_bgcolor){ } function sidebar_create($sidebar_title, $sidebar_bgcolor, $sidebar_height, $sidebar_width, $sidebar_border) { } } class sidebar_item extends sidebar { var $sidebar_item_url; var $sidebar_item_text; function sidebar_item() { } function set_url($input_url){ } function set_text($input_text){ } function sidebar_item_create($sidebar_item_url, $sidebar_item_text) { } } echo "test"; ?> Quote Link to comment Share on other sites More sharing options...
aschk Posted February 26, 2008 Share Posted February 26, 2008 It's an interesting example and I commend you on your hard work. Consider instead though, rather than putting all the parameters (such as height, width, etc) inside the class, let the html decide those (with CSS), and you instead concentrate on building the structure behind the navigation. i.e. given then a navigation system is nearly almost like a tree data structure. Abstract Nav_Element <?php abstract class Nav_Element { protected $name; protected $link; public function __construct($name = null, $link = null){ if(!is_null($name)){ $this->name = $name; } if(!is_null($link)){ $this->link = $link; } } public function getComposite(){ return null; } abstract public function getName(); abstract public function getLink(); } ?> Nav_Node <?php class Nav_Node extends Nav_Element { private $nodes = array(); public function addNav(Nav_Element $elem){ array_push($this->nodes, $elem); } public function getComposite(){ return $this; } public function getName(){ $ret = array(); foreach($this->nodes as $node){ $ret[$this->name][] = $node->getName(); } return $ret; } public function getLink(){ return $this->link; } } ?> Nav leaf <?php class Nav_Leaf extends Nav_Element { public function getName(){ return $this->name; } public function getLink(){ return $this->link; } } ?> Using the classes: <?php $navigation = new Nav_Node("Start"); if($navigation->getComposite()){ $node1 = new Nav_Node("Programs"); $navigation->addNav($node1); if($node1->getComposite()){ $node1->addNav(new Nav_Leaf("iTunes")); $node1->addNav(new Nav_Leaf("Zend Studio")); $node1->addNav(new Nav_Leaf("Carbide C++")); } $navigation->addNav(new Nav_Leaf("Search")); $navigation->addNav(new Nav_Leaf("Settings")); } print_r($navigation->getName()); ?> Quote Link to comment Share on other sites More sharing options...
Frenz Posted February 26, 2008 Author Share Posted February 26, 2008 I need to dissect that code for a while. Is this the intended output? Array ( [start] => Array ( [0] => Array ( [Programs] => Array ( [0] => iTunes [1] => Zend Studio [2] => Carbide C++ ) ) [1] => Search [2] => Settings ) ) Quote Link to comment Share on other sites More sharing options...
Frenz Posted February 26, 2008 Author Share Posted February 26, 2008 I understand most of that, though i will ask about the one thing that boggles me right now. public function abstract public function protected $var abstract class Other than those public/protected/abstract descriptors or what may they be, i understand it. And i will see if i can use that array to actually write a navigation bar, or will CSS actually format it all fine and dandy as the code stands right now? Also, i see that you extend each class upon the last one. And i see why you do it, but is this good practice? Was thinking up against workload or similar. Thanks for the example - Frenz Quote Link to comment Share on other sites More sharing options...
aschk Posted February 26, 2008 Share Posted February 26, 2008 In terms of memory allocation etc, there's no particular disadvantage to the process above. That may become different when you start pulling the information from a database. The getName() function returns an array, so you can loop that until you've got all the names out. It's also a nested array (tree heirarchy) so it can have infinite depth/width. Actually I only extend the Node and Leaf from the abstract class. The node however can contain other nodes/leaves, whereas a leaf is "end of tree" so can't contain anything. This follows the "composite" design pattern. The only problem I've got with what i've done is the mixing of keys and values. i.e. if the part is a leaf the value is the name, however is the part is a node, then the key is actually the name. All in all it was merely demonstration that you should segregate your navigation production logic from the display logic. In your original example you appeared to be mixing css (width/height/etc) with the production of the navigation itself. This is what i'm trying to dissuade. Quote Link to comment Share on other sites More sharing options...
Frenz Posted February 26, 2008 Author Share Posted February 26, 2008 As pure code as possible in classes then, ok. The code i chewed up was merely a syntax/thought process check. Thanks for helping me. Solved, for now. Quote Link to comment 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.