2024年PHP日本大会

DOMImplementation 类

(PHP 5, PHP 7, PHP 8)

简介

DOMImplementation 类提供了一些方法,用于执行独立于文档对象模型任何特定实例的操作。

类概要

class DOMImplementation {
/* 方法 */
public createDocument(?string $namespace = null, string $qualifiedName = "", ?DOMDocumentType $doctype = null): DOMDocument
public createDocumentType(string $qualifiedName, string $publicId = "", string $systemId = ""): DOMDocumentType|false
public hasFeature(string $feature, string $version): bool
}

目录

添加注释

用户贡献的注释 1 条注释

6
LANGE.LUDO
10年前
好的,我已经使用“代理模式”和特性使其完美运行。其思想是在“特性”中声明通用方法,以便扩展和注册的节点类即使不是扩展的DOMNode的派生/子类也可以访问它们……

这里有一小段代码
<?php
namespace my;

trait
tNode
{ // 我们需要 magic 方法 __get 来添加属性,例如 DOMNode->parentElement
public function __get($name)
{ if(
property_exists($this, $name)){return $this->$name;}
if(
method_exists($this, $name)){return $this->$name();}
throw new
\ErrorException('my\\Node property \''.(string) $name.'\' not found…', 42, E_USER_WARNING);
}

// parentElement 属性定义
private function parentElement()
{ if(
$this->parentNode === null){return null;}
if(
$this->parentNode->nodeType === XML_ELEMENT_NODE){return $this->parentNode;}
return
$this->parentNode->parentElement();
}

// JavaScript 等效项
public function isEqualNode(\DOMNode $node){return $this->isSameNode($node);}
public function
compareDocumentPosition(\DOMNode $otherNode)
{ if(
$this->ownerDocument !== $otherNode->ownerDocument){return DOCUMENT_POSITION_DISCONNECTED;}
$c = strcmp($this->getNodePath(), $otherNode->getNodePath());
if(
$c === 0){return 0;}
else if(
$c < 0){return DOCUMENT_POSITION_FOLLOWING | ($c < -1 ? DOCUMENT_POSITION_CONTAINED_BY : 0);}
return
DOCUMENT_POSITION_PRECEDING | ($c > 1 ? DOCUMENT_POSITION_CONTAINS : 0);
}
public function
contains(\DOMNode $otherNode){return ($this->compareDocumentPosition($otherNode) >= DOCUMENT_POSITION_CONTAINED_BY);}
}

class
Document extends \DomDocument
{ public function __construct($version=null, $encoding=null)
{
parent::__construct($version, $encoding);
$this->registerNodeClass('DOMNode', 'my\Node');
$this->registerNodeClass('DOMElement', 'my\Element');
$this->registerNodeClass('DOMDocument', 'my\Document');
/* [...] */
}
}

class
Element extends \DOMElement
{ use tNode;
/* [...] */
}

class
Node extends \DOMNode
{ use tNode;
/* [...] */
}

?>
To Top