I have a class AdminMenu that I'm using to create the menu for the admin on my project. Here is the class:
class AdminMenu{
public $menu = array(
'Groups and Statuses' => 'groups',
'Members Management' => 'members',
'Book Manager' => 'books',
'Activity Log' => 'activities',
'Quiz Maker' => 'quiz',
'Other' => 'other'
);
public function AdminMenu() {
$html = "<nav><ul><li><input type='text' id='search' placeholder='Search' /></li>";
foreach($this->menu as $text => $link){
$html .= '<li><a href="'. site_url(array('admin', $link)) .'">'. $text .'</a></li>';
}
$html .= '</ul></nav>';
echo $html;
}
}
As you can see, the constructor function is echo-ing out data, which is fine. So when I need the admin menu I can just call new AdminMenu(); and it appears great.
The thing is, CodeIgniter recommends keeping classes in the libraries directory and calling it like this : $this->load->library('adminMenu');. The problem is, for some reason it calls the constructor function here, so if I load it, then later create a new AdminMenu(), the constructor has been called a total of 2 times, so I have two menus.
Is loading of libraries meant to call the constructor and am I wrong here? Should I have created a different function for output? Or can I leave it in libraries and just call it like an included class?
Thanks!