Created
June 23, 2013 17:47
-
-
Save hakre/5845868 to your computer and use it in GitHub Desktop.
Extending DOMElement; Keep PHP Objects in Memory - http://stackoverflow.com/a/17262180/367456
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Extending DOMElement | |
* | |
* @linnk http://stackoverflow.com/q/17260190/367456 | |
*/ | |
/** | |
* Class ElementFactory | |
* | |
* WARNING: Demonstration Purposes Only! | |
*/ | |
class ElementFactory | |
{ | |
public $stack; | |
public function create($type, $name, $value = NULL) { | |
return $this->stack[] = new $type($name, $value); | |
} | |
} | |
class JavaScript extends DOMElement { | |
} | |
class JavaScriptLibrary extends DOMElement { | |
} | |
$dom = new DOMDocument(); | |
$factory = new ElementFactory(); | |
$node = $factory->create('JavaScript', 'script'); | |
$dom->appendChild($node); | |
$node = $factory->create('JavaScriptLibrary', 'script'); | |
$dom->appendChild($node); | |
$node = $factory->create('JavaScript', 'script'); | |
$dom->appendChild($node); | |
foreach ($dom->childNodes as $childNode) { | |
echo get_class($childNode)."\n"; | |
} | |
echo "\nUnset Stack:\n"; | |
unset($factory->stack); | |
foreach ($dom->childNodes as $childNode) { | |
echo get_class($childNode)."\n"; | |
} | |
echo "\nUnset Third Node Variable:\n"; | |
unset($node); | |
foreach ($dom->childNodes as $childNode) { | |
echo get_class($childNode)."\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment