Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading PHP classes from string

Tags:

php

I'm looking for a way to load classes into PHP without using hardwired names.

The idea is the script will load a text file with names of 'components'(classes), then load them by the names in the file. For example:

<xml><Classes><class name="myClass"/></Classes></xml>

When the PHP is run, it would need to do something like this:

require_once {myClass}".class.php";
var myclass = new {myClass}();
like image 973
Lee Loftiss Avatar asked Dec 15 '11 06:12

Lee Loftiss


1 Answers

require_once $class . ".class.php";
$myclass = new $class;

See http://www.php.net/manual/en/language.oop5.basic.php#language.oop5.basic.new.

like image 91
deceze Avatar answered Sep 22 '22 06:09

deceze