tags:

views:

57

answers:

4

How to create or define class in PHP at runtime and defin its attribute and functions? Is it possible?

A: 

No.

You can however, add properties dynamically to particular instances.

Saying that eval permits this is like saying in Java you could, problematically, write a source file, compile it, and load it (or, for that matter in PHP you could include it). Sure, it's possible, but it's not actually part of the language.

Artefacto
Well, actually, yes. But it's not recommendable.
zneak
@zneak I've edited.
Artefacto
A: 

you could create a generic extendable class and use the magic __set() and __get() methods to dynamically add properties. no methods though, sorry.

Scott M.
There is __call and __callStatic magic for overloading methods.
GWW
+4  A: 

You could, could, use eval.
But please, don't!

Meta-programming is not something PHP is particularly good at, so you should try to solve your problem without creating classes at runtime.

deceze
A: 

basically

 class A {
     function B() {...}
 }

everything in php is defined at runtime, so you should be fine with that. You can also try

  include 'some.class.php';

"include" is the same as "eval" but somehow has a better image.

stereofrog