how to declare a php class with a variable name

前端 未结 2 1409
时光取名叫无心
时光取名叫无心 2020-12-20 20:57

is there a way in php
to declare a class using a variable
ie (this doesnt work - but its here to give you an idea of my intention):

$myname = \"the_c         


        
2条回答
  •  天命终不由人
    2020-12-20 21:01

    Sure, use php to write to a file with the name you want, then require that file. You could also use eval, e.g. something like: eval("class $myname { ... };");

    For more complicated cases, use a library such as Zend's CodeGenerator (example here).

提交回复
热议问题