PHP Conference Japan 2024

DOMImplementation 類別

(PHP 5、PHP 7、PHP 8)

簡介

DOMImplementation 類別提供了一些方法,用於執行獨立於任何特定文件物件模型執行個體的操作。

類別概要

類別 DOMImplementation {
/* 方法 */
公開 createDocument(?字串 $namespace = null, 字串 $qualifiedName = "", ?DOMDocumentType $doctype = null): DOMDocument
公開 createDocumentType(字串 $qualifiedName, 字串 $publicId = "", 字串 $systemId = ""): DOMDocumentType|false
公開 hasFeature(字串 $feature, 字串 $version): 布林值
}

目錄

新增註記

使用者貢獻的註記 1 則註記

LANGE.LUDO
10 年前
好的,我使用带有 traits 的「代理模式」成功地讓它運作了。其概念是在「trait」內宣告常用的方法,以便即使不是繼承/子類別的擴展和註冊 Node 類別也能夠存取擴展的 DOMNode…

這裡是一個小片段
<?php
namespace my;

trait
tNode
{ // We need the magic method __get in order to add properties such as 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 property definition
private function parentElement()
{ if(
$this->parentNode === null){return null;}
if(
$this->parentNode->nodeType === XML_ELEMENT_NODE){return $this->parentNode;}
return
$this->parentNode->parentElement();
}

// JavaScript equivalent
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