dongyong6045 2016-03-20 21:51
浏览 39
已采纳

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

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

  • 写回答

1条回答 默认 最新

  • dongmeiba6151 2017-08-06 21:29
    关注

    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 "
    ";
    
    print( spl_object_hash( $ref2 = $container['test'] ) );
    print "
    ";
    

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端