FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » How to create an instance from a class name
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
How to create an instance from a class name [message #177809] Sun, 22 April 2012 01:23 Go to next message
Leonardo Azpurua is currently offline  Leonardo Azpurua
Messages: 46
Registered: December 2010
Karma: 0
Member
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.

--
Re: How to create an instance from a class name [message #177810 is a reply to message #177809] Sun, 22 April 2012 02:23 Go to previous messageGo to next message
Ross McKay is currently offline  Ross McKay
Messages: 14
Registered: January 2011
Karma: 0
Junior Member
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
Re: How to create an instance from a class name [message #177814 is a reply to message #177809] Sun, 22 April 2012 09:19 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
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.
Re: How to create an instance from a class name [message #177819 is a reply to message #177810] Sun, 22 April 2012 14:13 Go to previous messageGo to next message
Leonardo Azpurua is currently offline  Leonardo Azpurua
Messages: 46
Registered: December 2010
Karma: 0
Member
"Ross McKay" <au(dot)org(dot)zeta(dot)at(dot)rosko(at)invalid(dot)invalid> escribió en el mensaje
news:nnq6p7p82l6kp0dh4uetmejte0l4umd7so(at)4ax(dot)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!
Re: How to create an instance from a class name [message #177820 is a reply to message #177814] Sun, 22 April 2012 14:15 Go to previous message
Leonardo Azpurua is currently offline  Leonardo Azpurua
Messages: 46
Registered: December 2010
Karma: 0
Member
"M. Strobel" <sorry_no_mail_here(at)nowhere(dot)dee> escribió en el mensaje
news:9vi0pdF23nU1(at)mid(dot)uni-berlin(dot)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!

..
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: session question
Next Topic: Open Source Software Links
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Mon Nov 25 04:01:41 GMT 2024

Total time taken to generate the page: 0.02391 seconds