Why OO? WAS Re: [thelist] php design question

Max Kanat-Alexander maxka at cats.ucsc.edu
Tue Nov 19 18:54:06 CST 2002


--
[ Picked text/plain from multipart/alternative ]
At 11:34 AM 11/19/2002, you wrote:
>my menu items come out of the database, so i don't understand why
>i would then have to build them into objects

         Okay. Not to belabor this too much more, but I just had another idea.

         Say you want the menu items to DO different things on different pages.

         Say that you want one to turn red, one to turn grey, and the
seventh one down (whatever it is) to turn blue, because this is page number
seven. (Also, imagine that the colors that you want things to turn are
different for every page, adding up to several million combinations.) Then
you want them all to sort themselves in a different order than your SELECT
statement, based on what surrounds them.

         Now, imagine that you have, say, a thousand pages where this will
be the case.

         On this particular page, your menu items are:

         ItemOne
         ItemTwo
         ItemThree
         ItemFour
         ItemFive
         ItemSix
         ItemSeven
         ItemEight

         Include Way:
         On every page:
         1) turnGrey = "ItemOne"
         2) turnRed = "ItemTwo"
         3) BlueItem = pageNumber
         4) (Some custom sort function that I write before the include goes
here, most likely.)
         5) include the file



         Object Way
         1) Write a function for the menu object, called init(), which does
the above correctly based on the page you're in.
                 1a) Init will figure out what color each menu item should be.
                 1b) Init will call another function called
doCorrectMenuSort(), which you can write to do the right sort based on the
context of the object.
         2) Include the object (called "Menu").
         3) do Menu.init().

         Besides the simplicity, another advantage is:

         If you want to change what every page in the whole system does,
you just have to change how the init() function behaves. If you want to
make new pages that do something totally different, but keep the old init()
pages, you can write a new function called, say danceMenuDance().

         Once again, more useful in complex systems. And more useful in
application code than in web pages.

         -Max
--




More information about the thelist mailing list