|  | Posted by Rik on 06/23/07 00:25 
On Sat, 23 Jun 2007 00:18:56 +0200, <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=3Da, I
 > need an instance of PageA, but if I get the query: index.php?
 > content=3Db, 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 =3D isset($_GET["page"]) ? $_GET["page"] : "PageBase";
 > $thePage =3D eval("return new $pageType();");
 
 First of all, what is wrong with dropping the eval, and just creating  =
 
 objects on the fly? So:
 
 $pageType =3D (isset($_GET['page'] &&  =
 
 class_exists('Page'.strtoupper($_GET['page'][1]) ? 'Page'.$_GET['page'][=
 1]  =
 
 : 'PageBase';
 $thePage =3D new $pageType();
 
 > My question is whether a more object-oriented way exists to solve the
 > problem.
 
 Indeed, like others said, check Factory patterns.
 -- =
 
 Rik Wasmus
  Navigation: [Reply to this message] |