1

how can I declare new php class from string variable:

example:

$foo = 'Foo_Class';
if(!class_exists($foo)){
  class $foo extends Bar{}
}
J.Aries
  • 99
  • 7

1 Answers1

3

You can only do this by using eval:

$foo = 'Foo_Class';
if(! class_exists($foo)){
  eval("class $foo extends Bar{}");
}

But I wouldn't recommend this aproach.

Krac
  • 146
  • 6
  • I would like to try this, but what security holes Im dealing with? – J.Aries Apr 16 '16 at 13:56
  • So far this is the only i know. – J.Aries Apr 18 '16 at 04:20
  • @J.Aries Maybe not strictly PHP, but here you will get basic understanding why eval overall is a bad practice: https://blogs.msdn.microsoft.com/ericlippert/2003/11/01/eval-is-evil-part-one/ – Krac Apr 18 '16 at 13:23