douxigai8757 2010-03-07 23:23
浏览 47
已采纳

扩展DOMElement对象

How could I extend objects provided with Document Object Model? Seems that there is no way according to this issue.

class Application_Model_XmlSchema extends DOMElement
{
    const ELEMENT_NAME = 'schema';

    /**
     * @var DOMElement
     */
    private $_schema;

    /**
     * @param DOMDocument $document
     * @return void
     */
    public function __construct(DOMDocument $document) {
        $this->setSchema($document->getElementsByTagName(self::ELEMENT_NAME)->item(0));
    }

    /**
     * @param DOMElement $schema
     * @return void
     */
    public function setSchema(DOMElement $schema){
        $this->_schema = $schema;
    }

    /**
     * @return DOMElement
     */
    public function getSchema(){
        return $this->_schema;
    }

    /**
     * @param  string $name
     * @param  array $arguments
     * @return mixed
     */
    public function __call($name, $arguments) {
        if (method_exists($this->_schema, $name)) {
            return call_user_func_array(
                array($this->_schema, $name),
                $arguments
            );
        }
    }
}

$version = $this->getRequest()->getParam('version', null);
$encoding = $this->getRequest()->getParam('encoding', null);
$source = 'http://www.w3.org/2001/XMLSchema.xsd';

$document = new DOMDocument($version, $encoding);
$document->load($source);

$xmlSchema = new Application_Model_XmlSchema($document);
$xmlSchema->getAttribute('version');

I got an error:

Warning: DOMElement::getAttribute(): Couldn't fetch Application_Model_XmlSchema in C:\Nevermind.php on line newvermind

  • 写回答

3条回答 默认 最新

  • douan2907 2010-03-08 13:28
    关注

    Since getAttribute is already defined in DOMElement, your __call will not be used. As a consequence, any calls made to Application_Model_XmlSchema::getAttribute will go through the inherited DOMElement::getAttribute resulting in your problem.

    A quick workaround would be to get rid of extends DOMElement from the class definition and route calls through to DOMElement methods/properties with magic methods if you need that functionality: have your class act as a wrapper rather than child.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图