Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Object model design; base class awareness of derived classes
- abstract class AbstractNode{
- protected $_parent;
- public function __construct(self $parent = null){
- $this->_parent = $parent;
- }
- public function get_parent(){
- return $this->_parent;
- }
- }
- class NodeOne extends AbstractNode{ }
- class NodeTwo extends AbstractNode{ }
- // more derivatives
- public function get_node_one_ancestor(){
- if($this->_parent instanceof NodeOne){
- return $this->_parent;
- }
- if(null !== $this->_parent){
- return $this->_parent->get_node_one_ancestor();
- }
- return null;
- }
- <root>
- <foo id="1">
- <bar id="2"></bar>
- <bar id="3">
- <foo id="4">
- <bar id="5">
- <foo id="6">
- <bar id="7"></bar>
- </foo>
- </bar>
- <bar id="8"></bar>
- </foo>
- </bar>
- </foo>
- </root>
- public function get_ancestor($type){
- if($this->_parent instanceof $type){
- return $this->_parent;
- }
- if(null !== $this->_parent){
- return $this->_parent->get_ancestor($type);
- }
- return null;
- }
Add Comment
Please, Sign In to add comment