我想创建一个我不知道名字的类。在PHP中完成以下场景的最佳方法是什么?
$class_name = 'SomeClassName';
$code = "class {$class_name}_Control extends WP_Customize_Control {}";
eval( $code );
感谢。
答案 0 :(得分:0)
我刚试过这个,它运行正常。如果你只需要暂时上课,你总是可以在底部扔unlink()
。不需要exec()
。
<?php
// your dynamic classname
$fart = "poot";
// define your class, don't forget the "<?php"
$class = <<<YOYOYOHOMIE
<?php
class $fart{
public \$poop = "poop";
}
YOYOYOHOMIE;
// write the class to a file.
$filename = "dynamicClass".time().".php";
$fh = fopen($filename,"w+");
fwrite($fh, $class);
fclose($fh);
// require the file
require $filename;
// now your dynamically generated class is available
$tird = new $fart;
echo "<pre>";
var_dump($tird);