简体   繁体   English

为什么Pimple工厂方法会返回相同的实例?

[英]Why would Pimple factory method return same instance?

I am using the factory method of Pimple , but it's returning same instance every time. 我使用的是Pimple工厂方法 ,但每次都返回相同的实例。

$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 我在PHP 7.0.4上,而我的作曲家文件为pimple,标记为^3.0.0

Pimple does not return the same instance, but for some known reason those hashes are exactly the same. Pimple不会返回相同的实例,但是由于某些已知的原因,这些散列是完全相同的。 This is not something related to Pimple, but related to spl_object_hash and how PHP handles objects internally. 这与Pimple无关,但与spl_object_hash以及PHP如何在内部处理对象有关。 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())) ; 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. 单独运行它通常会产生相同的哈希值,因为PHP在第一个stdClass被解除引用并在创建第二个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. 您只需创建它们,打印它们的哈希值,然后PHP会将它们抛出内存不足。 For a better understanding of this note, try to keep those instances in memory by assigning them to variables ( $ref1 and $ref2 here): 为了更好地理解此注释,请尝试通过将这些实例分配给变量(此处$ref1$ref2 )来将其保留在内存中:

$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: spl_object_hash文档中还有一条说明

Note: 注意:

When an object is destroyed, its hash may be reused for other objects. 当一个对象被销毁时,它的哈希可以重用于其他对象。

So this is not some strange behavior. 所以这不是一些奇怪的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM