Kim Steinhaug
New Member
I am having problems extending a class correctly, or maby it is not possible?
Norsk går forsåvidt fint, er bare vant til engelsk
Example:
<?php
class extDOMDocument extends DOMDocument {
public function innerHTML($element){
$innerHTML = "";
$children = $element->childNodes;
foreach ($children as $child) {
$innerHTML .= $element->ownerDocument->saveHTML($child);
}
return $innerHTML;
}
}
$dom = new extDOMDocument('1.0');
$dom->loadHTML('<div id="first">First div</div><div id="second">Second div</div><div id="Third">Third div</div>');
// This works
echo $dom->innerHTML($dom->getElementById('second'));
// This would be better - but doesnt work.
echo $dom->getElementById('second')->innerHTML();
UPDATE: More thoughts around my OOP question
I understand the reason now, however do not know how to solve it. getElementById() creates a new DOMElement which is not extended from extDOMDocument or DOMDocument it seems - which I assumed. That explains the reason for the method not being found in my wanted functionality.
From what I read of this I could rewrite the function DOMDocument::getElementById() to use an extended version of DOMElement. Then again - I do not know what the code for the function DOMDocument::getElementById() is...
a) Is there a way to find the code for DOMDocument::getElementById so I could override it from my extDOMDocument
b) Can I extend a class - then override the original class with the extended one? This way I could extend the DOMElement with my innerHTML() function.
Interesting links that could have sollution, I tried however didnt succeed
http://stackoverflow.com/questions/2658906/is-there-any-way-to-redeclare-a-class-safely-in-php
Norsk går forsåvidt fint, er bare vant til engelsk
Example:
<?php
class extDOMDocument extends DOMDocument {
public function innerHTML($element){
$innerHTML = "";
$children = $element->childNodes;
foreach ($children as $child) {
$innerHTML .= $element->ownerDocument->saveHTML($child);
}
return $innerHTML;
}
}
$dom = new extDOMDocument('1.0');
$dom->loadHTML('<div id="first">First div</div><div id="second">Second div</div><div id="Third">Third div</div>');
// This works
echo $dom->innerHTML($dom->getElementById('second'));
// This would be better - but doesnt work.
echo $dom->getElementById('second')->innerHTML();
UPDATE: More thoughts around my OOP question
I understand the reason now, however do not know how to solve it. getElementById() creates a new DOMElement which is not extended from extDOMDocument or DOMDocument it seems - which I assumed. That explains the reason for the method not being found in my wanted functionality.
From what I read of this I could rewrite the function DOMDocument::getElementById() to use an extended version of DOMElement. Then again - I do not know what the code for the function DOMDocument::getElementById() is...
a) Is there a way to find the code for DOMDocument::getElementById so I could override it from my extDOMDocument
b) Can I extend a class - then override the original class with the extended one? This way I could extend the DOMElement with my innerHTML() function.
Interesting links that could have sollution, I tried however didnt succeed
http://stackoverflow.com/questions/2658906/is-there-any-way-to-redeclare-a-class-safely-in-php
Sist redigert: