PHP dynamic class loading

后端 未结 4 1881
深忆病人
深忆病人 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条回答
  • This works

    <?
    
    class UserVO
    {
        public $id;
        public $email;
    
        public function loadData($data)
        {
            $this->id = (int)$data['id'];
            $this->email = $data['email'];
        }
    }
    
    function create_vo($data, $vo)
    {
        $count = count($data);
        for ($i = 0; $i < $count; $i++)
        {
            $tmpObject = new $vo;
            $tmpObject->loadData($data[$i]);
            $result[] = $tmpObject;
            unset($tmpObject);
        }
        return $result;
    }
    
    
    $data = array();
    $data[] = array('id'=>1,'email'=>'data@example.com');
    $data[] = array('id'=>2,'email'=>'data@example.com');
    $data[] = array('id'=>3,'email'=>'data@example.com');
    $data[] = array('id'=>4,'email'=>'data@example.com');
    $data[] = array('id'=>5,'email'=>'data@example.com');
    $data[] = array('id'=>6,'email'=>'data@example.com');
    
    $result = create_vo($data,'UserVO');
    
    var_dump($result);
    
    ?>
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-05 23:20

    It appears that include_once()'s and require_once()'s performance is pretty bad.

    My suggestion:

    Kill all "require_once" and "include_once" and create an autoloader. Register this implementation

    0 讨论(0)
  • 2021-02-05 23:34
    $result[] = new $vo($data[$i]); //this obviously wont work...Class name must be a valid object or a string
    

    Have you tried it? It works just as expected (in php 5.1, I don't know how was it with OOP in php 4, but if you are using __construct for constructor this should work).

    To avoid multiple includes define magic function __autoload before using any class

    function __autoload($className)
    {
        require_once 'myclasses/'.$className.'.php';
    }
    

    So first call new UserVo will trigger this function to include file myclasses/UserVo.php.

    0 讨论(0)
提交回复
热议问题