If you need to access the private / protected class, you can simply use the magic__get method . In this case, the reflection would be overboard. Regardless of whether it uses reasonable sense to use magic methods in this case, it depends on your situation.
class MyClass
{
private $_items;
public function __get($prop)
{
if ($prop == '_items') {
return $this->_items;
}
throw new OutOfBoundsException;
}
}
UPDATE
, , . ArrayAccess $_items.
class MyClass implements ArrayAccess
{
private $_items = array();
public function __construct() {
$this->_items = array(
"one" => 1,
"two" => 2,
"three" => 3,
);
}
public function offsetSet($offset, $value) {
if (is_null($offset)) {
$this->_items[] = $value;
} else {
$this->_items[$offset] = $value;
}
}
public function offsetExists($offset) {
return isset($this->_items[$offset]);
}
public function offsetUnset($offset) {
unset($this->_items[$offset]);
}
public function offsetGet($offset) {
return isset($this->_items[$offset]) ? $this->_items[$offset] : null;
}
}
, , PHP ArrayObject , . $_items.