Skip to content

Commit a31ae60

Browse files
committed
Black hole pattern for element holder
1 parent d955c6c commit a31ae60

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

src/Element/ElementHolder.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@ public function get($key, $default = null)
4545
return parent::get($key, $default);
4646
}
4747

48+
/**
49+
* __get dynamically retrieves the value of an attribute, a safe object
50+
* is returned for missing keys to avoid indirect modification errors.
51+
* @param string $key
52+
* @return mixed
53+
*/
54+
public function __get($key)
55+
{
56+
$result = $this->get($key);
57+
58+
if ($result === null) {
59+
return new class {
60+
public function __get($k) { return $this; }
61+
public function __set($k, $v) {}
62+
};
63+
}
64+
65+
return $result;
66+
}
67+
4868
/**
4969
* getIterator for the elements.
5070
*/

0 commit comments

Comments
 (0)