Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #5649

Re: How to create an instance from a class name

Path csiph.com!usenet.pasdenom.info!gegeweb.org!newsfeed.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail
From "M. Strobel" <sorry_no_mail_here@nowhere.dee>
Newsgroups comp.lang.php
Subject Re: How to create an instance from a class name
Date Sun, 22 Apr 2012 11:19:41 +0200
Lines 90
Message-ID <9vi0pdF23nU1@mid.uni-berlin.de> (permalink)
References <jmvmje$rkj$1@dont-email.me>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-Trace news.uni-berlin.de rSiAJUtFmIVviKhsJTg37wMi0XC6VVhC5SNOYyOey8ePnM6a0=
User-Agent Mozilla/5.0 (X11; U; Linux x86_64; de; rv:1.9.2.28) Gecko/20120306 Thunderbird/3.1.20
In-Reply-To <jmvmje$rkj$1@dont-email.me>
Xref csiph.com comp.lang.php:5649

Show key headers only | View raw


Am 22.04.2012 03:23, schrieb Leonardo Azpurua:
> Hi,
> 
> I am working on a class to generate a report from specs contained in a user 
> defined file.
> 
> A section on the file defines a form in which the user will input parameters 
> for the report. This form may contain different kinds of INPUT items, namely 
> FECHA (date), NUMERO (number), TEXT, SELECT, LISTA (select with multiple 
> options) or CODIGO (an items code, with the ability to search by 
> name/description). Each item is defieed in a class that knows how to parse 
> the definitions and generate the corresponding HTML/Script code.
> 
> Each class name is made of a preffix ("procesador") plus the type of item 
> (e.g. "procesadorFECHA").
> 
> Initially I used a switch statement in order to build each class:
> 
> switch ($token->value)
> {
>     case "TEXT":
>         $proc = new procesadorTEXT();
>         break;
>     case "NUMBER":
>         $proc = new procesadorNUMERO();
>         break;
>     ...
>     default:
>         die("Invalid INPUT type ...");
> }
> $proc->go($tokens, $lineNumber, $this->context);
> 
> I recall having read somewhere an example of PHP instantiatiting an object 
> from a class name, but I forgot both the workings and the source of the 
> example. I made a few attempts with ReflectionClass, but I couldn't find a 
> solution, so this is what I did:
> 
>    $className = "procesador" . $token->value;
>    $newFunc = create_function("", "return new " . $className . "();");
>    $proc = $newFunc();
>    $proc->go($tokens, $lineNumber, $this->laEmpresa);
>    unset($newFunc);
> 
> It works, but I would like to know whether it will produce any unexpected 
> behaviour when the component enters in production, and if there is a better 
> (cleaner) way to do it.
> 
> Thanks.
> 
> --

Just take the classname as variable in the new Operator:

class clase1 {
  function buenos() {
    echo "Bon día!\n";
  }
}

class clase2 {
  function buenos() {
   echo "Buenos días!\n";
  }
}

foreach (array(1,2) as $v) {
    $cn = 'clase' . $v;
    $c = new $cn();
    $c->buenos();
}
#--------------------
Bon día!
Buenos días!


/Str.

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

How to create an instance from a class name "Leonardo Azpurua" <leonardo@exmvps.org> - 2012-04-21 20:53 -0430
  Re: How to create an instance from a class name Ross McKay <au.org.zeta.at.rosko@invalid.invalid> - 2012-04-22 12:23 +1000
    Re: How to create an instance from a class name "Leonardo Azpurua" <leonardo@exmvps.org> - 2012-04-22 09:43 -0430
  Re: How to create an instance from a class name "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-04-22 11:19 +0200
    Re: How to create an instance from a class name "Leonardo Azpurua" <leonardo@exmvps.org> - 2012-04-22 09:45 -0430

csiph-web