PHP domelement - insert 上的运行方法


php domelement - run method on insert?

好吧,我或多或少寻找的是 php 中的事件处理程序...... - 我想。

我正在编写一个 php 脚本来制作一个 xml 文件(确切地说是 ansi n42.42 文件)我想做的是能够定义各种"子树"并将它们合并到最终的 DOMDocument 中。我将每个子树定义为扩展的 DOMNode,具有构建子树所需的属性。由于 domnode 在插入 DOMDocument 之前是不可变的,因此我正在定义一个构建方法,在我插入它以设置内容后运行该方法:

例如,在我的课堂上:(为简洁起见,删除了一些部分)

class calibration extends DOMElement{
  var $calibtype="Energy";
  var $coefficients;
  var $id;
  function calibration($coeff,$id=false){
    parent::__construct('Calibration');
    $this->coefficients=$coeff;
    $this->id=$id;
  }
  function build(){ // must be called after the calibration is inserted into a dom-tree.
    $this->setAttribute('Type',$this->calibtype);
    $DOM=$this->ownerDocument;
    $node=$DOM->createElement('Equation');
  }

然后使用它:

$cal = new calibration("-4, 3.12","cal_id");
$n42->firstChild->appendChild($cal);  // n42 is the object extending DOMDocument
$cal->build();

$cal附加到树中时,是否有可能以某种方式使$cal->build()自动运行?

(或者我可能吠错了树,我从一开始就使用 DOMFragment 会更好吗?

如果我

理解正确,那么您可以将观察者添加到您的脚本中,他们将负责更改 DOMDocument。那是如果我理解正确的话。