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;
//...
?>
$manager
stores a reference to the menu manager (Menu
object). This makes us able to use menu manager methods withinItem
context.$id
stores the item’s id.$pid
stores item’s parent id if it has one otherwise it’ll be set tonull
.$meta
an array for storing extra data with each item.$attributes
an array of html attributes.$link
stores 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