PHP: How to instantiate a class with arguments from within another class

后端 未结 6 628
死守一世寂寞
死守一世寂寞 2021-02-07 13:48

I am in a situations where i need to instantiate a class with arguments from within an instance of another class. Here is the prototype:

//test.php

class test
{         


        
6条回答
  •  半阙折子戏
    2021-02-07 14:09

    you need Reflection http://php.net/manual/en/class.reflectionclass.php

    if(count($args) == 0)
       $obj = new $className;
    else {
       $r = new ReflectionClass($className);
       $obj = $r->newInstanceArgs($args);
    }
    

提交回复
热议问题