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

Posted by grossespinne on 06/22/07 22:18

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

 

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

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