Note that there are some explanatory texts on larger screens.

plurals
  1. POunderstanding pimple php source code
    primarykey
    data
    text
    <p>Pimple is a simple dependency injection container in php used in silex framework. I was going through the source code <a href="https://github.com/fabpot/Pimple/blob/master/lib/Pimple.php" rel="nofollow" title="Pimple">here</a>. In the documentation the function <code>offsetGet</code> returns the same instance of the class that is attached to the dependency container. the relevant code for <code>offsetGet</code> is :</p> <pre><code>public function offsetGet($id) { if (!isset($this-&gt;keys[$id])) { throw new InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id)); } if ( isset($this-&gt;raw[$id]) || !is_object($this-&gt;values[$id]) || isset($this-&gt;protected[$this-&gt;values[$id]]) || !method_exists($this-&gt;values[$id], '__invoke') ) { return $this-&gt;values[$id]; } if (isset($this-&gt;factories[$this-&gt;values[$id]])) { return $this-&gt;values[$id]($this); } $this-&gt;frozen[$id] = true; $this-&gt;raw[$id] = $this-&gt;values[$id]; return $this-&gt;values[$id] = $this-&gt;values[$id]($this); } </code></pre> <p>Here, if the object is in the <code>factories</code> Object Store(<code>SplObjectStorage</code> type), it returns a new instance of the class with id $id. then in the last return again <code>$this-&gt;values[$id]</code> is set to a new instance of the object and that new instance is returned.</p> <p><code>return $this-&gt;values[$id] = $this-&gt;values[$id]($this)</code>. </p> <p>This is the line I fail to understand. How is this line supposed to return the same instance for different calls of <code>offsetGet</code> 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. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload