Pimple is a simple dependency injection container in php used in silex framework. I was going through the source code here. In the documentation the function offsetGet
returns the same instance of the class that is attached to the dependency container. the relevant code for offsetGet
is :
public function offsetGet($id)
{
if (!isset($this->keys[$id])) {
throw new InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id));
}
if (
isset($this->raw[$id])
|| !is_object($this->values[$id])
|| isset($this->protected[$this->values[$id]])
|| !method_exists($this->values[$id], '__invoke')
) {
return $this->values[$id];
}
if (isset($this->factories[$this->values[$id]])) {
return $this->values[$id]($this);
}
$this->frozen[$id] = true;
$this->raw[$id] = $this->values[$id];
return $this->values[$id] = $this->values[$id]($this);
}
Here, if the object is in the factories
Object Store(SplObjectStorage
type), it returns a new instance of the class with id $id. then in the last return again $this->values[$id]
is set to a new instance of the object and that new instance is returned.
return $this->values[$id] = $this->values[$id]($this)
.
This is the line I fail to understand. How is this line supposed to return the same instance for different calls of offsetGet
for the same $id. Won't it return a new instance every time?
Please help me. I tried a lot but I don't get it.