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 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理