. Nested scope | dikini.net
 

Nested scope

Intent
Create a composable runtime hierarchy of data and behaviour
Motivation
We want dynamic means of modifying complex runtime structures
Implementation
Use php magic to dynamically dispatch requests to parent methods
Examples

class scope {
  var $parent;
  function __call( $name ) {
    if(isset($parent[$name]) { return $parent[$name](); }
    throw( new someException() );
  }

  function let( $name, $value ) {
    if(is_object($value)) { 
      //hygiene
      if(isset($this->$name)) { $this->$name->parent = null; }
      $this->$name = $value;
      $this->$name->parent = $this;
  }
}

//alternative let
function let($parent, $name, $class) {
  $parent->$name = new $class();
  $parent->$name->parent = array($parent,$name);
}

?>

Notes
  • A concept related to and very often mistaken (mixed) (by me) with (for) closures
  • the let method can be folded into __set if we want this behaviour to the default for assignments to object fields
Powered by Drupal, an open source content management system