|
Posted by Jerry Stuckle on 06/23/07 02:14
grossespinne@gmail.com wrote:
> Hi everybody!
> I have been thinking over the following problem:
> there are three classes: PageBase, which is the base class, PageA and
> PageB which are the subclasses of PageBase.
> In the index.php file I have a variable which I like to hold either an
> instance of PageA or PageB depending on the query string that is
> passed to index.php (e.g: if I get the query index.php?content=a, I
> need an instance of PageA, but if I get the query: index.php?
> content=b, an instance of PageB is needed)
>
> Until now I found two solutions, but I am not satisfied with either of
> them:
> - use a switch structure
> - or use eval:
>
> $pageType = isset($_GET["page"]) ? $_GET["page"] : "PageBase";
> $thePage = eval("return new $pageType();");
>
> My question is whether a more object-oriented way exists to solve the
> problem.
>
> TIA
>
Not every statement has to be object oriented. That's why PHP still has
switch statements. It's what I would do.
There are other ways to do it, i.e. factories. But I also believe in KISS.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|