PHP[OOP] - How to call class constructor manually?

前端 未结 6 486
时光取名叫无心
时光取名叫无心 2020-12-30 07:47

Please see the code bellow:

01. class Test {
02.     public function __construct($param1, $param2, $param3) {
03.         echo $param1.$para         


        
6条回答
  •  执念已碎
    2020-12-30 08:03

    I don't know if there are some security concerns by using the eval() method, but you could make yourself a function like this:

    function CreateObj($className,$params)
    {
        $strArgs = '$params['.implode('],$params[',array_keys($params)).']';
        eval('$ob = new '.$className.'('.$strArgs.');');
        return $ob
    }
    

    And now $ob should now be defined with its correct parameters, i haven't tested it and maybe there is a mistake in the code, but you get the idea....

提交回复
热议问题