【问题标题】:How do I obtain an iterative object reference to the root object in PHP?如何在 PHP 中获取对根对象的迭代对象引用?
【发布时间】:2015-02-25 22:25:26
【问题描述】:

我有一个类,它采用一些基本的 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 反向遍历一个类,但答案似乎是否定的。

In PHP is it possible to reverse traverse a Traversable class to find the root object?

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

我将专注于包装建议。

更新

SimpleXML 实际上不是真正的对象,而是系统资源。这可以解释我的痛苦来自哪里。说笔记:http://php.net/manual/en/class.simplexmlelement.php

所以对于所有对象来说,感觉就像是一串对象相互调用,但实际上并非如此。

【问题讨论】:

  • 我认为我在这个问题上做错了什么?除非得到指示,否则我无法学习。
  • 等一下,你可能会得到答案。
  • RememberThisBitAs("foo");是什么意思?
  • 在每个元素中保留对顶级工厂的引用对您有用吗?
  • @MatthewBrown 这正是我回答的选项 1。

标签: php oop design-patterns simplexml


【解决方案1】:

选项 1:引用根

您可以修改 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() - 我只需要尝试看看。
  • 看起来我真正需要做的不仅仅是扩展 SimpleXMLElement(我已经完成了,而是实际上创建了一个委托类,我认为这就是你一直在说的。@987654321 @
【解决方案2】:

鉴于 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);
    }
}

【讨论】:

    猜你喜欢
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    • 2022-11-15
    相关资源
    最近更新 更多