Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #5642 > unrolled thread
| Started by | "Leonardo Azpurua" <leonardo@exmvps.org> |
|---|---|
| First post | 2012-04-21 20:53 -0430 |
| Last post | 2012-04-22 09:45 -0430 |
| Articles | 5 — 3 participants |
Back to article view | Back to comp.lang.php
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
| From | "Leonardo Azpurua" <leonardo@exmvps.org> |
|---|---|
| Date | 2012-04-21 20:53 -0430 |
| Subject | How to create an instance from a class name |
| Message-ID | <jmvmje$rkj$1@dont-email.me> |
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.
--
[toc] | [next] | [standalone]
| From | Ross McKay <au.org.zeta.at.rosko@invalid.invalid> |
|---|---|
| Date | 2012-04-22 12:23 +1000 |
| Message-ID | <nnq6p7p82l6kp0dh4uetmejte0l4umd7so@4ax.com> |
| In reply to | #5642 |
On Sat, 21 Apr 2012 20:53:58 -0430, Leonardo Azpurua wrote: >I am working on a class to generate a report from specs contained in a user >defined file. >[...] >I recall having read somewhere an example of PHP instantiatiting an object >from a class name, [...] $className = 'ExampleClass'; $instance = new $className; Can be quite handy in Singletons, e.g. $className = __CLASS__; $instance = new $className; -- Ross McKay, Toronto, NSW Australia "Let the laddie play wi the knife - he'll learn" - The Wee Book of Calvin
[toc] | [prev] | [next] | [standalone]
| From | "Leonardo Azpurua" <leonardo@exmvps.org> |
|---|---|
| Date | 2012-04-22 09:43 -0430 |
| Message-ID | <jn13lh$5tc$1@dont-email.me> |
| In reply to | #5643 |
"Ross McKay" <au.org.zeta.at.rosko@invalid.invalid> escribió en el mensaje news:nnq6p7p82l6kp0dh4uetmejte0l4umd7so@4ax.com... > On Sat, 21 Apr 2012 20:53:58 -0430, Leonardo Azpurua wrote: > >>I am working on a class to generate a report from specs contained in a >>user >>defined file. >>[...] >>I recall having read somewhere an example of PHP instantiatiting an object >>from a class name, [...] > > $className = 'ExampleClass'; > $instance = new $className; > > Can be quite handy in Singletons, e.g. > > $className = __CLASS__; > $instance = new $className; That's why I couldn't find it. Thanks!
[toc] | [prev] | [next] | [standalone]
| From | "M. Strobel" <sorry_no_mail_here@nowhere.dee> |
|---|---|
| Date | 2012-04-22 11:19 +0200 |
| Message-ID | <9vi0pdF23nU1@mid.uni-berlin.de> |
| In reply to | #5642 |
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.
[toc] | [prev] | [next] | [standalone]
| From | "Leonardo Azpurua" <leonardo@exmvps.org> |
|---|---|
| Date | 2012-04-22 09:45 -0430 |
| Message-ID | <jn13q4$6r3$1@dont-email.me> |
| In reply to | #5649 |
"M. Strobel" <sorry_no_mail_here@nowhere.dee> escribió en el mensaje
news:9vi0pdF23nU1@mid.uni-berlin.de...
> 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!
Too easy to be remembered!
Thanks, obrigado, gracias!
.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web