Skip to content

Instantly share code, notes, and snippets.

@Simplesmente
Created October 26, 2016 00:08
Show Gist options
  • Save Simplesmente/3ab6440129c89dd1d24008579c467f75 to your computer and use it in GitHub Desktop.
Save Simplesmente/3ab6440129c89dd1d24008579c467f75 to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
namespace MeuFramework\Container;
use ArrayAccess;
/**
* @author André Teles
*/
class Container implements ArrayAccess
{
protected $items = [];
protected $cache = [];
public function offsetSet($offset, $value)
{
$this->items[$offset] = $value;
}
/**
* @return Callble
* @param string $offset
*/
public function offsetGet($offset)
{
if( !$this->has($offset) ){
return false;
}
$item = $this->items[$offset]($this);
return $item;
}
/**
* @return void
* @param string $offset
*/
public function offsetUnset($offset)
{
if( $this->has($offset) ){
unset($this->items[$offset]);
}
}
/**
* @return void
* @param string $offset
*/
public function offsetExists($offset)
{
return isset( $this->items[$offset] );
}
private function has(string $offset):bool
{
return $this->offsetExists($offset);
}
public function __get( string $property)
{
return $this->offsetGet($property);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment