为什么 Pimple 工厂方法会 return 同一个实例?
Why would Pimple factory method return same instance?
我正在使用 factory method of 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'] ) );
这是我不期望的确切行为,因为方法的定义说它每次都提供一个新实例。
我使用的是 PHP 7.0.4,我的 pimple 作曲家文件标记在 ^3.0.0
Pimple 与 return 不同,但出于某些 已知 原因,这些哈希值完全相同。这与 Pimple 无关,但与 spl_object_hash 以及 PHP 如何在内部处理对象有关。引用 this user contributed note,回答您问题的部分以粗体显示:
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.
所以这是因为您没有保留对 returned 对象的引用。您只需创建它们,打印它们的哈希值,然后 PHP 将它们抛出内存。为了更好地理解本说明,请尝试通过将这些实例分配给变量(此处为 $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";
spl_object_hash
文档中还有一个 note 说:
Note:
When an object is destroyed, its hash may be reused for other objects.
所以这不是什么奇怪的行为。
我正在使用 factory method of 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'] ) );
这是我不期望的确切行为,因为方法的定义说它每次都提供一个新实例。
我使用的是 PHP 7.0.4,我的 pimple 作曲家文件标记在 ^3.0.0
Pimple 与 return 不同,但出于某些 已知 原因,这些哈希值完全相同。这与 Pimple 无关,但与 spl_object_hash 以及 PHP 如何在内部处理对象有关。引用 this user contributed note,回答您问题的部分以粗体显示:
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.
所以这是因为您没有保留对 returned 对象的引用。您只需创建它们,打印它们的哈希值,然后 PHP 将它们抛出内存。为了更好地理解本说明,请尝试通过将这些实例分配给变量(此处为 $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";
spl_object_hash
文档中还有一个 note 说:
Note:
When an object is destroyed, its hash may be reused for other objects.
所以这不是什么奇怪的行为。