あなたができることは、DOMElement
(属性ミューテータメソッドを定義するクラス)を拡張して、DOMDocument
で登録してください。
もう少し気楽にしたい場合は、DOMDocument
を拡張して、デフォルトでこの動作を組み込むことができます。たとえば、自分のネームスペースDom
;
ドム\要素:
namespace Dom;
class Element
extends \DOMElement
{
// I think something like this would make
// a little more sense than your original example
// that would probably set all attributes to the same value
public function setAttributes(array $attributes) {
foreach($attributes as $name => $value) {
$this->setAttribute($name, $value);
}
}
}
ドム\ドキュメント:
namespace Dom;
class Document
extends \DOMDocument
{
public function __construct($version = '1.0', $encoding = null) {
parent::__construct($version, $encoding);
// we register our custom DOMElement class
// you can do this for all DOM classes
// that are derivatives of DOMNode
$this->registerNodeClass('DOMElement', 'Dom\Element');
}
}
使用例:取り込んでカスタム関数を定義することができなく何
$doc = new Dom\Document;
$scriptElement = $doc->createElement('script');
$scriptElement->setAttributes(array(
'defer' => '',
'async' => '',
'src' => $instagram_js_path
));
変数を呼び出し、yそれが必要ですか? – Pyromonk