Skip to content
Advertisement

Is there a method in PHP to use a varible as all or part of a classname?

Example

$append = 'News';

class My_$append_Component {

}

The goal is to have a dynamic variable which changes the appended string based on folder names found in a directory.

Advertisement

Answer

It is possible to use PHP to write a file that contains a class definition and then include or require that file. The newly created class could have a name based on a variable. So I think it’s possible although I can’t think of any reason you would want to do this.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement