1

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

Dave Stein
  • 8,653
  • 13
  • 56
  • 104
  • Updated versions above. So really it can be either major version releases... I'm gonna try rolling to 2.8 on Pimple first – Dave Stein Mar 20 '16 at 22:51
  • @RyanVincent are you on another version? I don't see 2.8 anywhere: https://github.com/silexphp/Pimple/releases – Dave Stein Mar 20 '16 at 22:56
  • Sorry, I use the latest version as it is PHP 5.3 compatible: https://github.com/silexphp/Pimple. Here is the output from the test program: http://pastebin.com/Zs3879Bc – Ryan Vincent Mar 21 '16 at 06:51
  • They are not same instances, but their hashes are the same, and yes, if two objects do not exist at the same time (like your example where the first object is destroyed before the second one is created) [it is possible](http://php.net/manual/en/function.spl-object-hash.php#refsect1-function.spl-object-hash-notes) for them to have same hash. – Nima Jul 21 '18 at 17:21

1 Answers1

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:

Note that the contents (properties) of the object are NOT hashed by the function, merely its internal handle and handler table pointer. This is sufficient to guarantee that any two objects simultaneously co-residing in memory will have different hashes. Uniqueness is not guaranteed between objects that did not reside in memory simultaneously, for example:

var_dump(spl_object_hash(new stdClass()), spl_object_hash(new stdClass()));

Running this alone will usually generate the same hashes, since PHP reuses the internal handle for the first stdClass after it has been dereferenced and destroyed when it creates the second stdClass.

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 ($ref1 and $ref2 here):

$container = new \Pimple\Container();

$container['test'] = $container->factory(function( $c ) {
  $services = new \Pimple\Container();

  return $services;
} );

// Outputs different object hashes
print( spl_object_hash( $ref1 = $container['test'] ) );
print "\n";

print( spl_object_hash( $ref2 = $container['test'] ) );
print "\n";

There is also a note in spl_object_hash documentation that says:

Note:

When an object is destroyed, its hash may be reused for other objects.

So this is not some strange behavior.

Nima
  • 3,309
  • 6
  • 27
  • 44