dongzi9196 2010-12-12 22:41
浏览 49
已采纳

创建我自己的(非数据库)fetch_object函数

In php mysql / mysqli / postgre / etc... there are fetch_object functions where you can get an object for your row of data. By default it will return an object of stdClass, but you can also define a class_name and an array of params for the constructor.

I would like to do the same thing with a plain set of values. Preferably, setting the properties of the object before calling the constructor, which is the same behaviour the database-functions show. However, this doesn't seem to be possible.

The only way to even create an object with properties set seems to be to unserialize a preconstructed string. But that example still creates a new object as well, then sets the properties of that object from the unserialized object to ensure the constructor is called. But this means the constructor is called before the properties are set.

In short: I would like the following:

array_fetch_object(array $properties, string $class_name [, array $params ])

with the constructor called after the properties are set.

  • 写回答

3条回答 默认 最新

  • dsfsfsfsfs55656 2010-12-17 00:23
    关注

    In the end I wrote the following class which performs the task using unserializing a fabricated string. It uses reflection to determine what the access-type of properties is and what the name of the constructor is (if any).

    The heart of the class is the following line:

    $object = unserialize('O:'.strlen($class_name).':"'.$class_name.'"'.substr(serialize($properties), 1));
    

    which serializes the property-array and renames the serialized to the desired class_name.

    class ObjectFactory {
    
        private $properties;
        private $constructors;
    
        public function __construct() {
            $this->properties   = array();
            $this->constructors = array();
        }
    
        private function setClass($class_name) {
    
            $class = new ReflectionClass($class_name);
            $this->properties[$class_name] = array();
    
            foreach($class->getProperties() as $property) {
    
                $name     = $property->getName();
                $modifier = $property->getModifiers();
    
                if($modifier & ReflectionProperty::IS_STATIC) {
                    continue;
                } else if($modifier & ReflectionProperty::IS_PUBLIC) {
                    $this->properties[$class_name][$name] = $name;
                } else if($modifier & ReflectionProperty::IS_PROTECTED) {
                    $this->properties[$class_name][$name] = "\0*\0".$name; // prefix * with \0's unserializes to protected property
                } else if($modifier & ReflectionProperty::IS_PRIVATE) {
                    $this->properties[$class_name][$name] = "\0".$class_name."\0".$name; // prefix class_name with \0's unserializes to private property
                }
            }
    
            if($constructor = $class->getConstructor()) {
                $this->constructors[$class_name] = $constructor->getName();
            }
        }
    
        private function hasClassSet($class_name) {
    
            return array_key_exists($class_name, $this->properties);
        }
    
        private function hasClassProperty($class_name, $property_name) {
    
            if(!$this->hasClassSet($class_name))
                $this->setClass($class_name);
    
            return array_key_exists($property_name, $this->properties[$class_name]);
        }
    
        private function getClassProperty($class_name, $property_name) {
    
            if(!$this->hasClassProperty($class_name, $property_name))
                return false;
    
            return $this->properties[$class_name][$property_name];
        }
    
        private function hasClassConstructor($class_name) {
    
            if(!$this->hasClassSet($class_name))
                $this->setClass($class_name);
    
            return $this->constructors[$class_name] !== false;
        }
    
        private function getClassConstructor($class_name) {
    
            if(!$this->hasClassConstructor($class_name))
                return false;
    
            return $this->constructors[$class_name];
        }
    
        public function fetch_object(array $assoc, $class_name = 'stdClass', array $params = array()) {
    
            $properties = array();
    
            foreach($assoc as $key => $value) {
                if($property = $this->getClassProperty($class_name, $key)) {
                    $properties[$property] = $value;
                }
            }
    
            $object = unserialize('O:'.strlen($class_name).':"'.$class_name.'"'.substr(serialize($properties), 1));
    
            if($constructor = $this->getClassConstructor($class_name)) {
                call_user_func_array(array($object, $constructor), $params);
            }
    
            return $object;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程