nested elements / property inheritance

I’m not at all sure where I’m going with this, but it’s an interesting little experiment.

Perhaps :slight_smile:




<?php


class Element

{

  protected $_parent = null;

  protected $_props = array();

  

  public function __construct(Element $parent=null)

  {

    $this->_parent = $parent;

  }

  

  public function __get($name)

  {

    $element = $this;

    

    do

    {

      if (array_key_exists($name, $element->_props))

        return $element->_props[$name];

    }

    while ($element = $element->_parent);

    

    throw new Exception("undefined property $name");

  }

  

  public function __set($name, $value)

  {

    $this->_props[$name] = $value;

  }

  

  public function __unset($name)

  {

    unset($this->_props[$name]);

  }

}


// --- EXAMPLE:


$a = new Element();

$b = new Element($a); // $b is nested in $a

$c = new Element($<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='B)' />; // $c is nested in $b


// (so we now have a heriarchy like $a -> $b -> $c)


$a->test = 'Hello';


var_dump($a->test);

var_dump($b->test); // inherits from $a

var_dump($c->test); // inherits from $a (via $<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='B)' />


$b->test = 'World';


var_dump($a->test); // retains it's previous value

var_dump($b->test); // now has it's own value

var_dump($c->test); // inherits from $b


$c->test = 'Hello, World!';


var_dump($c->test); // $c now has it's own value


unset($c->test);


var_dump($c->test); // $c now inherits from $b again



I’m trying to think of techniques and new approaches to content management, and it seems a natural part of every CMS is associations to tree-structures of some kind - which is also the nature of HTML and (to some extend) the web in general.

What if HTML was constructed using some kind of document model that supported inheritance?

Obviously this is a very limited example, but if an Element had something equivalent to the ID and CLASS attributes used in HTML, perhaps those could be used to filter or control the property inheritance in some way.

As said, I’m not really sure what I’m trying to do here, but I thought it was interesting enough to share :wink: