php - How to do method chaining with DOMDocument? -
do method chaining with php easy. need this,
$xml = $dom->transformtothis('file1.xsl')->transformtothis('file2.xsl')->savexml(); or
$books = $dom-> transformtothis('file1.xsl')-> transformtothis('file2.xsl')-> getelementsbytagname('book'); it possible php's domdocument or domnode?
class domxx extends domdocument { public function __construct() { parent::__construct("1.0", "utf-8"); } function trasformtothis($xslfile) { $xsldom = new domdocument('1.0', 'utf-8'); $xsldom->load($xslfile); $xproc = new xsltprocessor(); $xproc->importstylesheet($xsldom); $this = $xproc->transformtodoc($this); // error! return $this; } } // class the $this = x invalid construct in php, , not understand workaround explained here. can use $this->loadxml( $xproc->transformtodoc($this)->savexml() ); big overload, , question how correct thing.
another (wrong) way try implement,
function trasformtothis($xslfile) { ... same ... return $xproc->transformtodoc($this); // lost trasformtothis() method } so, in case question "how cast domxx?".
how this:
class domxx { function __construct($version = '1.0', $encoding = 'utf-8') { $this->document = new domdocument($version, $encoding); } function __call($name, $args) { $callback = array($this->document, $name); return call_user_func_array($callback, $args); } function transformtothis($xslfile) { $xsldom = new domdocument('1.0', 'utf-8'); $xsldom->load($xslfile); $xproc = new xsltprocessor(); $xproc->importstylesheet($xsldom); $this->document = $xproc->transformtodoc($this->document); return $this; } } instead of extending domdocument, keep internal reference domdocument object inside domxx class. majority of methods forwarded internal object via __call method. , transformtothis method updates reference new document returned transformtodoc call.
Comments
Post a Comment