Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP分析、处理某些对象并将其转换为stdClass_Php - Fatal编程技术网

PHP分析、处理某些对象并将其转换为stdClass

PHP分析、处理某些对象并将其转换为stdClass,php,Php,在我们的后台,我们有一个从数据库生成模型的核心系统。当返回一个对象时,我们创建一个stdClass实例。查询结果中的所有列都设置为property,当处理完查询结果时,stdClass对象将转换为(我们称之为)Decorator类。基本上,Decorator类有一个property$\u对象,stdClass存储在其中。我们这样做是为了获得对动态创建对象的控制。这一切都很好 但是,我正在使用SOAP开发Web服务器。webservice返回整个Decorator对象(可能有子对象,也可能是Dec

在我们的后台,我们有一个从数据库生成模型的核心系统。当返回一个对象时,我们创建一个stdClass实例。查询结果中的所有列都设置为property,当处理完查询结果时,stdClass对象将转换为(我们称之为)Decorator类。基本上,Decorator类有一个property$\u对象,stdClass存储在其中。我们这样做是为了获得对动态创建对象的控制。这一切都很好

但是,我正在使用SOAP开发Web服务器。webservice返回整个Decorator对象(可能有子对象,也可能是Decorator对象,以及子对象..等等)。这种结构与我们的内部系统配合得非常好,因为我们可以控制Decorator对象,但是对于外部世界,我想将Decorator对象结构还原为stdClass实例,其中子类也是stdClass。基本上,我想删除包含Decorator的打印结果中的所有“节点”

如何实现我想要的任何想法(见下面的结果)。PHP的
get\u object\u vars
没有返回任何内容,实际上我被卡住了

我的样本数据:

Decorator Object
(
    [oClass:Decorator:private] => stdClass Object
        (
            [Id] => 1
            [FAQCategoryId] => 1
            [TitleId] => 1
            [ContentId] => 2
            [Views] => 226
            [DateCreated] => 2011-10-31 11:17:44
            [DateModified] => 
            [Title] => My title..
            [Content] => My content..
            [AttachmentSet] => Array
                (
                    [0] => Decorator Object
                        (
                            [oClass:Decorator:private] => stdClass Object
                                (
                                    [Id] => 1
                                    [LanguageId] => 1
                                    [FAQItemId] => 1
                                    [Attachment] => file1.pdf
                                )

                        )

                    [1] => Decorator Object
                        (
                            [oClass:Decorator:private] => stdClass Object
                                (
                                    [Id] => 2
                                    [LanguageId] => 1
                                    [FAQItemId] => 1
                                    [Attachment] => file2.pdf
                                )

                        )

                )
        )
)
我想把它转换成:

stdClass Object
(
    [Id] => 1
    [FAQCategoryId] => 1
    [TitleId] => 1
    [ContentId] => 2
    [Views] => 226
    [DateCreated] => 2011-10-31 11:17:44
    [DateModified] => 
    [Title] => My title..
    [Content] => My content..
    [AttachmentSet] => Array
        (
            [0] => stdClass Object
                (
                    [Id] => 1
                    [LanguageId] => 1
                    [FAQItemId] => 1
                    [Attachment] => file1.pdf
                )
            [1] => stdClass Object
                (
                    [Id] => 2
                    [LanguageId] => 1
                    [FAQItemId] => 1
                    [Attachment] => file2.pdf
                )
        )
)

我已经弄明白了。在我的例子中,我创建了一个函数,返回Decorator对象的stdObject。在我的Controller_核心类中,我创建了递归处理给定对象并将整个结构作为一个stdClass返回的函数

我的代码,如果对某人有帮助:

/**
 * Controller_Core::RevertToStdClass
 *
 * @params: Decorator   $oObject
 * @return: stdClass    $oObject
 **/
public function RevertToStdClass(Decorator $oObject)
{   
    if(is_a($oObject, "Decorator"))
    {
        $oObject = $oObject->ReturnStdObject();
    }

    $aProperties = get_object_vars($oObject);

    foreach($aProperties as $sProperty => $mValue)
    {
        if(is_array($mValue))
        {
            foreach($mValue as $mIndex => $mSubValue)
            {
                if(is_a($mSubValue, "Decorator"))
                {
                    $oObject->{$sProperty}[$mIndex] = $this->RevertToStdClass($mSubValue);
                }
            }
        }
        else
        {
            if(is_a($mValue, "Decorator"))
            {
                $oObject->{$sProperty} = $this->RevertToStdClass($oObject->{$sProperty});
            }
        }
    }

    return $oObject;    
}