is there a way in php
to declare a class using a variable
ie (this doesnt work - but its here to give you an idea of my intention):
$myname = "the_class";
class $myname {
...
}
i also tried:
define("MYNAME","the_class");
class MYNAME{
...
}
and i tried:
$myname = "the_class";
class $$myname {
...
}
this doesnt really help:
http://php.net/manual/en/language.variables.scope.php
nor does this:
http://us3.php.net/manual/en/keyword.class.php
thanks very much for your help
Sure, use php to write to a file with the name you want, then require that file. You could also use eval
, e.g. something like: eval("class $myname { ... };");
For more complicated cases, use a library such as Zend's CodeGenerator
(example here).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With