J.Aries
J.Aries

Reputation: 99

Declare class from Variable in PHP

how can I declare new php class from string variable:

example:

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

Upvotes: 1

Views: 96

Answers (1)

Krac
Krac

Reputation: 164

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.

Upvotes: 3

Related Questions