PHP dynamic class loading

后端 未结 4 1947
深忆病人
深忆病人 2021-02-05 23:09

Lets say that I have an array that I want to convert to a value object.

My value object class is as follows:

/* file UserVO.php*/
 class UserVO
 {
    pu         


        
4条回答
  •  感动是毒
    2021-02-05 23:14

    First, conversion of data to an array object for UserVO should be done with ArrayObject

    so

    class UserVO extends ArrayObject
    {
    }
    

    You are trying to use the factory method pattern and your code seems to be right, though you seem to have an forgotten to define $result as an array ($result = array()).

    You can also use the ReflectionClass to pass constructor arguments as well as such:

    $rc = new ReflectionClass($vo);
    $rc->newInstanceArgs($data[$i]);
    

    To "autoload" your UserVO object, you should use the spl_autoload_register function with a php include path.

提交回复
热议问题