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 grossespinne on 06/24/07 10:35

On Jun 23, 4:14 am, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> grossespi...@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.
> jstuck...@attglobal.net
> ==================

Thanks to everybody for your help. I think I am going to use the
Factory method and implement it with the solution suggested by Rik.

 

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

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