[thelist] php design question

s t e f notabene at members.evolt.org
Tue Nov 19 13:03:01 CST 2002


rudy wrote:
> if i want to use an include, i go "include navbar.txt"
>
> if i want to use a class, i say "dance, nav bar, dance" and go
> "navbar.show()"
>
> i still don't see the difference, but boy oh boy, i do hope they ask me this
> on my next job interview

Here's a practical example I had to do a few days ago:

A nice menu was to be created. This menu incorporated multi-level nested
divs. I won't write here a succession of nest-upon-nest-upon-nest. This
menu was dhtml-driven, so each sub-div should show/hide through a
manipulation of its display properties, right?

And this div should check if its parent is displayed, and if not display
it. Thus the code becomes bloated with "onclick" statements all around.
Yeah I know in pure DOM you can grab events, but hey, it's for the sake
of explanation, right?

If I had to write it in pure HTML, every new div added would have been a
headache: how do I keep count of the id of each div, of each of its
parents, where does that div end again, etc.

You see the problem so far rudy?

With PHP-OO capabilities (although rudimentary), you can externalize the
whole menu constructor in an include. This include contains a class,
Menu, and functions (the methods of this class, to compare it to any
other OO-based language).

Thus my menus can be edited by almost anyone in my team without much
hassle. The menu goes :

$menu = new Menu( ... here I pass the menu's name, classes, whatever ...);

$menu->addLink("text","url");
[repeat as many times as needed]

$menu->addSubMenu("this is my first submenu",["optional URL"]);
   $menu->addLink("text,"url");
   $menu->addSubMenu("this menu is nested inside the first submenu",[]);
     $menu->addLink("text","url");
     $menu->addSubMenu("this one is 3-lvl deep");
       $menu->addLink("text","url");
       $menu->addLink("text","url");
     $menu->closeSubMenu();
     $menu->addLink("yet aonther link","url");
   $menu->closeSubMenu();
   $menu->addLink("bla","url");
   $menu->addSubMenu("this one is at level 2",[]);
     $menu->addLink("text","url");
   $menu->closeSubMenu();
   $menu->addLink("bla","url");
$menu->closeSubMenu();

$menu->addSubMenu("level-one submenu",["optional URL"]);
   $menu->addLink("text,"url");
   $menu->addLink("text,"url");
$menu->closeSubMenu();

$menu->printToBrowser();

Still bearing with me rudy?

What you read above is much, much clearer than plain HTML (obviously
maintenance comes to mind), and enables me to _externalize_ all the
final formatting, be it the <div></div> thing or the <a href>'ing or not
of a submenu's title, but also counting automatically so that each new
div created gets a unique id made up of the menu's name PLUS the
counter. Less maintenance hassle.

Plus you could then very easily plug an SQL recursive query that would
populate the menus/submenus directly with this object, because its
methods are simple to write out.

I'll bring it back from work and put it online so that you can see the
benefits of objects in this case.

Is it clearer rudy? Do you see the advantages?




More information about the thelist mailing list