Last Updated: February 25, 2016
·
1.771K
· ravinsharma7

Experimentation with data-flow structures with PHP

Lately, I have been experiment and delving into data-flow techniques in PHP. I sometimes get a bit woozy with OOB, my brain has kept on bugging me on some other way to express code and computation using classes. I wanted a way to express and change my code logic in a seemless manner. This is rough structure which I came up with.

POC: Abstraction of program sequence and data states.

interface iStateHolder{}
interface iNode{}
class StateHolder implements iStateHolder{
  public function StateHolder($state){
    if(is_array($state))
    {
      foreach($state as $key => $state){
        $this->$key = $state;
      }
    }
    else{
        $this->state = $state;
    }

  }
}
class Node implements iNode{

  private $creation_time, $state_object, $_return;

  public function Node(iStateHolder &$state = null){    
      $this->creation_time = time();  
      $this->state_object = $state;
  }

  public function foo1(){
     // do stuff
     $this->_return = $return_data = "foo1 return data";
     return $this;
  }

  public function foo2(){
    // do stuff 
    $this->_return = $return_data = "foo2 return data";
    return $this;
  }
  public function foo3(){
    // do stuff 
    $this->_return = $return_data = "foo3 return data";
    return $this;
  }
  public function link(iNode $adjacent_node){
    $adjacent_node->_return = $this->_return; 
  }

  public function state_object(){
    return $this->state_object;
  }

  public function return_data(){
    return $this->_return;
  }

  public function creation_time(){
    return $this->creation_time();
  }
}

$node1 = new Node(new StateHolder(
    array(
    'ip' => "127.0.0.1",
    'host_name' => 'ravin',
    'sample' => 'watever'
  )
));
$node2 = new Node();
$node1->foo1()->foo2()->foo3()->link($node2);

// program sequence is abstracted, and pass data to node2 via link method
$node1->foo3()->foo2()->foo1()->link($node2);
$node2->foo1()->foo1()->foo2();

// holds the state object passed  by reference
var_dump($node1->state_object());
// node2 has the return data of foo2()
var_dump($node2);