I need an idea to create anonymous class on PHP. I don't know how I can works.
See my limitations:
class {}
);class $name {}
);runkit
PECL.What I need, and why:
Well, I need create a function called ie create_class()
that receives a key name and a anonymous class. It'll be useful for me because I want use different name class symbols that PHP can't accept. For instance:
<?php
create_class('it.is.an.example', function() {
return class { ... }
});
$obj = create_object('it.is.an.example');
?>
So, I need an idea that accept this use. I need it because on my framework I have this path: /modules/site/_login/models/path/to/model.php
. So, the model.php
need to declare a new class called site.login/path.to.model
.
On call create_object()
if the internal cache have a $class
definition (like it.is.an.example
it simply return the new class object. If not, need load. So I will use the $class
content to search fastly what is the class file.
If you really need to to that, you could use eval()
$code = "class {$className} { ... }";
eval($code);
$obj = new $className ();
But the gods won't approve this. You will go to hell if you do it.
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