I am using the factory method of Pimple, but it's returning same instance every time.
$container = new \Pimple\Container();
echo '<pre>';
$container['test'] = $container->factory(function( $c ) {
$services = new \Pimple\Container();
return $services;
} );
// Both outputs string(32) "0000000061066681000000005c9b6294"
var_dump( spl_object_hash( $container['test'] ) );
var_dump( spl_object_hash( $container['test'] ) );
It's the exact behavior I don't expect given the definition of the method saying it gives a new instance every time.
I'm on PHP 7.0.4 and my composer file for pimple is marked at ^3.0.0
Pimple does not return the same instance, but for some known reason those hashes are exactly the same. This is not something related to Pimple, but related to spl_object_hash and how PHP handles objects internally. Quoting this user contributed note, the part that answers your question is in bold:
So this is because you're not keeping a reference to returned objects. You just create them, print their hashes and then PHP throws them out of memory. For a better understanding of this note, try to keep those instances in memory by assigning them to variables (
$ref1and$ref2here):There is also a note in
spl_object_hashdocumentation that says:So this is not some strange behavior.