Skip to content

Instantly share code, notes, and snippets.

@muthu32
Created November 4, 2017 11:29
Show Gist options
  • Save muthu32/32ef9ee5bdead18d450f7b1907957930 to your computer and use it in GitHub Desktop.
Save muthu32/32ef9ee5bdead18d450f7b1907957930 to your computer and use it in GitHub Desktop.
<?php
class storage
{
protected $data;
public function __construct($data)
{
$this->data = $data;
}
public function __call($method, $arguments)
{
if(true === array_key_exists($method, $this->data))
{
if(is_array($this->data[$method]) && count($arguments) > 0)
{
if(true === array_key_exists($arguments[0], $this->data[$method]))
{
return new self($this->data[$method][$arguments[0]]);
}
}
return $this->data[$method];
}
}
}
$data = array(
'users' => array(
1 => array(
'name' => 'sample',
'age' => 21
),
5 => array(
'name' => 'sample',
'age' => 25,
'books' => array(
1 => array(
'title' => 'Moby Dick'
)
)
)
)
);
$storage = new storage($data);
echo $storage->users(1)->age(); #21
echo $storage->users(5)->age(); #25
echo $storage->users(5)->books(1)->title(); #Moby Dick
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment