I want to create a class of which I don't know the name. What is the best way to accomplish the following scenario in PHP?
$class_name = 'SomeClassName';
$code = "class {$class_name}_Control extends WP_Customize_Control {}";
eval( $code );
Thanks.
I just tested this and it works fine. And if you only need the class temporarily you can always throw an unlink()
at the bottom. No exec()
required.
<?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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.