Skip to content

Instantly share code, notes, and snippets.

@omnidan
Created January 30, 2012 14:13
Show Gist options
  • Save omnidan/1704601 to your computer and use it in GitHub Desktop.
Save omnidan/1704601 to your computer and use it in GitHub Desktop.
PHP-References (Pointers) used in the TouchNet
<?php
/**
* These classes maintain references (pointers) in PHP. The first class
* can store one reference only, while the ReferenceContainer can store many
* references at once.
*
* This class is used in the TouchNet to send all modules to all modules.
*
* @author Daniel Bugl
*/
class TN_Reference {
public $v = null;
function __construct($variable) {
$this->v = $variable;
}
}
/**
* ALWAYS use the ReferenceContainer if you do not know how to use References
* correctly!
*/
class TN_ReferenceContainer {
private $references = array();
public function addPointer($name, $data) {
if (is_object($data))
$obj = $data;
else
$obj = new TN_Reference($data);
if (get_class($obj) == "TN_Reference")
$ref = $obj;
else
$ref = new TN_Reference($obj);
$this->references[$name] =& $ref;
}
public function getPointers() {
$ret = array();
reset($this->references); // Set the index to 0
while ($pointer = current($this->references)) { // Loop through the keys
$ret[] = key($this->references);
next($this->references);
}
return $ret;
}
private function getPointerID($name) {
/*for ($i=0; $i<=count($this->references); $i++) {
if($this->references[$i]["name"] == $name) return $i;
}*/
return $name; // The structure of references changed a bit.
// TODO: Remove this useless function.
}
public function getPointer($name) {
$id = $this->getPointerID($name);
return $this->references[$id];
}
public function delPointer($name) {
$id = $this->getPointerID($name);
unset($this->references[$id]);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment