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

How do I obtain an iterative object reference to the root object in PHP?

我有一个 class 需要一些基本的 HTML5 和一些 DOM 魔法将它变成 class 这是一个简单的扩展 XML元素。这一切都始于一个叫做 XPage 的 "factory"(我可能有点滥用这个术语)class。

因此我们可能

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

到目前为止一切顺利。我现在需要做的是 select XML 文档的某些部分,以便它们可以是 "bookmarked" (我相信它有一个更好的名字)这样就可以了这个

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

理想情况下我希望能够去

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

或直观上类似的东西。然后应该发生的是 object $MyPage(类型为 XPage)应该传递引用,在本例中为 html/body/header(这可能不是正确的 XPath 表示法,但希望您能遵循) .

想法是可以更顺畅地到达最常访问的区域。这就是 SimpleXML class(即使有我的扩展)遇到的事情,如果没有另一个工厂,它就无法做到,并且还使 XPage 成为一个单例。

然而,保存 "bookmarks" 数组的是 XPage class ($MyPage)。据我所知,没有办法将引用传递给 SimpleXML 的迭代,这样 child 或 child 的 child 就无法告诉工厂"bookmark me please"。

这会让我做一些令人讨厌的事情,例如:

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

这似乎不对。

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

还不错,但仍然 "feels" 错误。

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

更新

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

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

我将重点关注包装建议。

更新

SimpleXML 实际上不是真正的 object 而是系统资源。这可以解释我的痛苦从何而来。说出注释:http://php.net/manual/en/class.simplexmlelement.php

因此,对于所有 object 感觉就像一串 object 互相调用,但实际上并非如此。

选项 1:引用 root

您可以将 XPage class 的方法修改为 return XPage 对象而不是 simpleXMLElements(或另一个 class 扩展 simpleXMLElement)。然后 class 可以有一个额外的 属性 持有对 "root" 元素的引用。

所以基本上你的 "wrapper class" 看起来像这样(前面是伪代码!):

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);

在 Lars Ebert 的回答的巨大推动下,在考虑了一些疯狂(且不可行)的想法之前,我想到了 Extend Class with Final Constructor on PHP 这让我明白我需要的设计模式是代表团 class.

因为我可能还没有完成向 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);
    }
}