In part 1, we prototyped the end product and wrote the main Menu class, which serves as the menu manager - a container to hold all sub-units (items and links). In this part, we’ll build the remainder of the classes and demonstrate the menu builder’s usage.
Item
Represents our menu items as independent objects.
Create a new file called item.php and paste in the following code:
item.php
<?php
class Item {
protected $manager;
protected $id;
protected $pid;
protected $meta;
protected $attributes = array();
public $link;
//...
?>
$managerstores a reference to the menu manager (Menuobject). This makes us able to use menu manager methods withinItemcontext.$idstores the item’s id.$pidstores item’s parent id if it has one otherwise it’ll be set tonull.$metaan array for storing extra data with each item.$attributesan array of html attributes.$linkstores an instance of classLink.
__construct(manager, title, url, attributes, pid)
Initializes the attributes.
<?php
public function __construct($manager, $title, $url, $attributes = array(), $pid = 0)
{
$this->manager = $manager;
$this->id = $this->id();
$this->pid = $pid;
$this->title = $title;
$this->attributes = $attributes;
// Create an object of type Link
$this->link = new Link($title, $url);
}
?>
add(title, options)
Class Item has an add() method as well (just like the menu manager). In fact this method doesn’t create items on its own. It gets the arguments, a
Truncated by Planet PHP, read more at the original (another 3742 bytes)
more
{ 0 comments... » Dynamic Menu Builder for Bootstrap 3: Item and Link read them below or add one }
Post a Comment