Skip to content

Instantly share code, notes, and snippets.

@Renegade334

Renegade334/test.php Secret

Created May 19, 2015
Embed
What would you like to do?
ArrayAccess and isset()
<?php
error_reporting(E_ALL);
class TestClass implements ArrayAccess {
protected $container = [];
public function offsetExists($index) {
echo "offsetExists(" . var_export($index, true) . ")\n";
return isset($this->container[$index]);
}
public function offsetGet($index) {
echo "offsetGet(" . var_export($index, true) . ")\n";
return $this->container[$index];
}
public function offsetSet($index, $value) {
echo "offsetSet(". var_export($index, true) . ", " . var_export($value, true) . ")\n";
$this->container[$index] = $value;
}
public function offsetUnset($index) {
echo "offsetUnset(" . var_export($index, true) . ")\n";
unset($this->container[$index]);
}
}
$obj = new TestClass;
echo "* Calling isset(\$obj['foo'])...\n";
var_dump(isset($obj['foo']));
echo "* Calling isset(\$obj['foo']['bar'])...\n";
var_dump(isset($obj['foo']['bar']));
echo "* Setting offset...\n";
$obj['foo'] = null;
echo "* Calling isset(\$obj['foo']['bar'])...\n";
var_dump(isset($obj['foo']['bar']));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment