📜  PHP | DOMAttr __construct()函数

📅  最后修改于: 2022-05-13 01:56:29.984000             🧑  作者: Mango

PHP | DOMAttr __construct()函数

DOMAttr::__construct()函数是PHP中的一个内置函数,用于创建新的 DOMAttr 对象。此创建的对象是只读类型。

句法:

public DOMAttr::__construct( string $name, string $value )

参数:该函数接受上面提到的两个参数,如下所述:

  • $name:此参数保存属性的元素名称。
  • $value:此参数保存属性的值。

下面的程序说明了PHP中的 DOMAttr::__construct()函数:

方案一:

appendChild($rootElement);
  
// Create an attribute
$domAttr = new DOMAttr('attr', 'GeeksforGeeks');
  
// Set the attribute to the node
$attr = $element->setAttributeNode($domAttr);
  
// Display the XML document
echo $domDocument->saveXML(); 
  
?>
输出:


方案二:

appendChild($rootElement);
   
// Create an attribute
$domAttr1 = new DOMAttr('Name', 'GeeksforGeeks');
  
// Set the attribute to the node
$attr = $element->setAttributeNode($domAttr1);
   
// Create an attribute
$domAttr2 = new DOMAttr('Address', 'Noida');
  
// Set the attribute to the node
$attr = $element->setAttributeNode($domAttr2);
   
// Create an attribute
$domAttr3 = new DOMAttr('mail', 'abc@geeksforgeeks.org');
  
// Set the attribute to the node
$attr = $element->setAttributeNode($domAttr3);
   
// Display the XML document
echo $domDocument->saveXML(); 
   
?>
输出:


参考: https://www. PHP.net/manual/en/domattr.construct。 PHP