You are here: Re: Instantiate subclass depending on string value « PHP Programming Language « IT news, forums, messages
Re: Instantiate subclass depending on string value

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация