Dynamic class name in PHP -


i'm trying create system has generalobj. generalobj allows user initiate special objects database's tables string of table name. far, works perfect.

class generalobj{     function __construct($tablename) {         //...     } } 

however, tired type new generalobj(xxx) every time. wondering possible simplify process new xxx(), running same new generalobj(xxx)? spot php provided __autoload method dynamic loading files in setting include_path requires definition file existing. don't want copy , copy same definition files changing little.

for cause, eval not option.

maybe can auto-create files in autoloader:

function __autoload($class_name) {     // check classes ending 'table'     if (preg_match('/(.*?)table/', $class_name, $match)) {         $classpath = path_to_tables . '/' . $match[1] . '.php';         // auto-create file         if (!file_exists($classpath)) {             $classcontent = " class $class_name extends generalobj {     public __construct() {         parent::__construct('{$match[1]}');     } }";             file_put_contents($classpath, $classcontent);         }         require_once $classpath;     } } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -