在PHP中如何获取根对象的迭代对象引用?

8

我有一个类,它接收一些基本的HTML5代码,并通过DOM技术将其转换为扩展simpleXMLElement的类。这一切都始于名为XPage的“工厂”类(我可能有点滥用这个术语)。

因此,我们可能会有:

<?php
$MyPage = new XPage($HTML);

目前为止做得很好。现在我需要做的是选择XML文档的一些部分,以便可以将它们“加为书签”(我相信有更好的名称),这样就可以像这样进行操作。

$MyPage->ThatBit("foo")->addChild('p',$MyParagraph);

理想情况下,我想要能够进行操作。
$MyPage->page()->body->header->RememberThisBitAs("foo");

或者类似的直觉。然后应该发生的是,对象$ MyPage(类型为XPage)应该传递此处html / body / header的引用(这可能不是完全正确的XPath符号,但希望您能理解)。

这样可以更流畅地访问最常用的区域。这就是SimpleXML类(即使使用我的扩展)遇到需要另一个工厂和将XPage设置为单例才能完成的任务。

然而,正是XPage类($ MyPage)保存了“书签”的数组。据我所知,没有办法在SimpleXML的迭代中向下传递引用,以便子级或子级的子级无法告诉工厂“请给我加个书签”。

这会让我做一些不好的事情,例如:

$MyPage->addBookmark("foo", 
$MyPage->page()->body->header->getChildren() );

这似乎不太对。

或者

$MyPage->page()->body->header->RememberThisBitAs("foo",$MyPage);

虽然不算太糟糕,但仍然感觉“不对劲”。

我该如何在不过度耦合对象或使用丑陋的自引用的情况下解决这个问题?

更新

我想知道是否可以使用debug_backtrace反向遍历类,但答案似乎是否定的。

在PHP中,是否可能反向遍历可遍历类以找到根对象?

其他类似的测试已经告诉我,在可遍历堆栈中的任何其他元素中都无法访问在元素上设置类变量的值。

我将专注于包装器建议。

更新

SimpleXML实际上并不是真正的对象,而是系统资源。这可能解释了我的困扰所在。请参阅说明:http://php.net/manual/en/class.simplexmlelement.php

因此,尽管所有对象都感觉像是互相调用的一系列对象,但实际上并非如此。


我猜我在这个问题上做错了什么?除非有人指导,否则我无法学习。 - Matthew Brown aka Lord Matt
1
请耐心等待,您很可能会得到答案。 - ForceBru
1
RememberThisBitAs("foo"); 的含义是什么? - hek2mgl
1
在每个元素中保留对顶部工厂的引用对您是否有帮助? - RealSkeptic
1
@MatthewBrown 这正是我答案中的选项1。 - Lars Ebert
显示剩余3条评论
2个回答

4

选项一:引用根元素

您可以修改XPage类的方法,以返回XPage对象,而不是simpleXMLElements(或另一个扩展simpleXMLElement的类)。该类可以具有一个额外的属性,保存对“根”元素的引用。

因此,您的“包装器类”将类似于以下内容(伪代码):

class Wrap extends simpleXMLElement
{
    private $root = null;
    private $refs = array();

    public function addReference($name, $element = null) {
        if($element === null) {
            $element = $this;
        }
        if($this->root === null) {
            $this->refs[$name] = $this;
        } else {
            $this->root->addReference($name, $this);
        }
    }

    public function getReference($name) {
        if($this->root === null) {
            return $this->refs[$name];
        } else {
            return $this->root->getReference($name);
        }
    }
}

因此,您上面提到的用例将如下所示:

$MyPage->page()->body->header->addReference("foo");

$MyPage->getReference("foo")->addChild('p',$MyParagraph);

选项2:添加id

或者,您可以为该元素添加id属性,并通过该属性检索它:

$MyPage->page()->body->header->addAttribute("id", "foo");

$MyPage->xpath("//*[@id='foo']")->addChild('p',$MyParagraph);

使用选项一,我们遇到了我认为可能会出现的问题 - 我不确定如何使扩展的simpleXML对象将父引用传递到迭代链下,并且不确定是否包装每个simpleXML方法是完全正确的。我想我可以使用__call() - 我只需要尝试并查看。 - Matthew Brown aka Lord Matt
看起来我真正需要做的不仅是扩展SimpleXMLElement(我已经做了),而是创建一个委托类,这就是你一直在说的东西。https://dev59.com/R1LTa4cB1Zd3GeqPZ2G2 - Matthew Brown aka Lord Matt

0

鉴于Lars Ebert的回答所带来的巨大推动,以及在考虑了一些疯狂(且不可行)的想法之后,我找到了Extend Class with Final Constructor on PHP,从而了解到我需要的设计模式是委托类。

由于我可能还没有完成向SimpleXMLElement扩展添加专业功能,因此我使用了魔术方法来创建委托包装器。缺点是这在我的IDE中表现不佳,但在初始测试中完全有效。我已经剔除了仅与我的项目相关的内容,但否则这就是我正在使用的解决方案。

它要求XPage对象实现一些相同的接口元素(我可能会定义一个接口),并使用我的扩展和指向自身的指针将SimpleXML元素传递到此委托包装器中,该包装器的行为类似于SimpleXML,但确实是一个对象,并且可以将调试消息和自引用传递回XPage。

<?php

/**
 * A Delegation extension of SimpleXMLElement
 * 
 * Designed to act like SimpleXML but builds a back traceable tree that expects
 * XPage (or some other parent factory) to catch references and debug messages.
 * 
 * @author Lord Matt 
 * 
 * @license
 *  Copyright (C) 2015 Matthew Brown
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

class AEWrapper {

    protected $mother = null;
    protected $MyAE = null;

    public function __construct($MyAE,$mother) {
        $this->MyAE = $MyAE;
        $this->mother = $mother;
        return $this;
    }

    public function __call($name, $arguments) {
        try {
            $result = call_user_method_array($name, $this->MyAE, $arguments);
        } catch (Exception $e) {
            $this->log_error('AEWrapper caught exception: ',  $e->getMessage());
            return FALSE;
        }
        if(is_object($result)){
            return new AEWrapper($result,$this);
        }
        return $result;
    }

    public function __get($name) {
        try {
            $result = $this->MyAE->{$name};
        } catch (Exception $e) {
            $this->log_error('AEWrapper caught exception: ',  $e->getMessage());
            return FALSE;
        }
        if(is_object($result)){
            return new AEWrapper($result,$this);
        }
        return $result;
    }

    public function &actual_simpleXML_obj_please(){
        return $this->MyAE;
    }

    // methods exists in XPage
    public function register_as($name,$what=null){
        if($what===null){
            $what = $this;
        }
        $this->mother->register_as($name,$what);
    }

    // [SNIP] 
    // Other project specific stuff here
    // [/SNIP]

    // methods exists in XPage    
    protected function log_error($error){
        $this->mother->log_error('/'.$error);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接