5

我正在尝试获取 PHP 5+ 中对象实例的唯一 ID。

该功能spl_object_hash()可从 PHP 5.2 获得,但我想知道是否有适用于旧 PHP 版本的解决方法。

php.net 上的评论中有几个函数,但它们对我不起作用。第一个(简化):

function spl_object_hash($object){
    if (is_object($object)){
        return md5((string)$object);
        }
    return null;
    }

不适用于本机对象(例如 DOMDocument),第二个:

function spl_object_hash($object){
    if (is_object($object)){
        ob_start();
        var_dump($object);
        $dump = ob_get_contents();
        ob_end_clean();
        if (preg_match('/^object\(([a-z0-9_]+)\)\#(\d)+/i', $dump, $match)) {
            return md5($match[1] . $match[2]);
            }
        }
    return null;
    }

看起来它可能是一个主要的性能破坏者!

有没有人袖手旁观?

4

4 回答 4

4

我进行了几次快速测试。我真的认为你最好在你的 bind() 函数中使用bind('evt_name', array($obj, 'callback_function')). 如果您绝对想走 spl_object_hash 路线,而不是使用事件绑定存储引用,那么您正在查看如下内容:

一个 var_dump / extract 和 hash id 实现:

function spl_object_hash_var_dump($object){
    if (is_object($object)){
        ob_start();
        var_dump($object);
        $dump = ob_get_contents();
        ob_end_clean();
        if (preg_match('/^object\(([a-z0-9_]+)\)\#(\d)+/i', $dump, $match)) {
            return md5($match[1] . $match[2]);
            }
        }
    return null;
}

一个天真的引用实现:

function spl_object_dumb_references(&$object) {
    static $hashes;

    if (!isset($hashes)) $hashes = array();

    // find existing instance
    foreach ($hashes as $hash => $o) {
        if ($object === $o) return $hash;
    }

    $hash = md5(uniqid());
    while (array_key_exists($hash, $hashes)) {
        $hash = md5(uniqid());
    }

    $hashes[$hash] = $object;
    return $hash;
}

这个基本上比基于类的引用函数差 5-50 倍,所以不值得担心。

按类实现的存储引用:

function spl_object_hash_references(&$object) {
    static $hashes;

    if (!isset($hashes)) $hashes = array();

    $class_name = get_class($object);
    if (!array_key_exists($class_name, $hashes)) {
        $hashes[$class_name] = array();
    }

    // find existing instance
    foreach ($hashes[$class_name] as $hash => $o) {
        if ($object === $o) return $hash;
    }

    $hash = md5(uniqid($class_name));
    while (array_key_exists($hash, $hashes[$class_name])) {
        $hash = md5(uniqid($class_name));
    }

    $hashes[$class_name][$hash] = $object;
    return $hash;
}

您最终会得到如下所示的结果。总结:基于类的引用实现在 n/50 个类附近表现最好——在最好的情况下,它设法降低了var_dump基于实现的 1/3 的性能,而且通常差。

var_dump实施似乎是可以容忍的,但并不理想。但是,如果您没有进行太多此类查找,那么它不会成为您的瓶颈。特别是作为 PHP < 5.2 boxen 的后备。

于 2010-03-17T01:58:51.613 回答
1

我曾经为 wordpress 编写了一个帮助函数,它为每个对象提供一个唯一的哈希值,它与计数器一起工作,并将哈希值存储为公共类属性(如果它已分配给一个对象)。以下示例演示了这一点:

/**
 * get object hash
 *
 * Returns a unique hash per object.
 *
 * Proxy function for wordpress installments on servers
 * with a PHP version < 5.2.0.
 *
 * @since 3.0.2
 * @note Become deprecated with version 3.2.0 (PHP 5.2 requirements)
 * @param object $object
 * @return string unique object hash
 */
function wp_object_hash( &$object ) {
    static $prefix, $count = 0, $property = '__wphookobjhash__', $spl_function_exists;

    isset( $spl_function_exists ) || $spl_function_exists = function_exists( 'spl_object_hash' );

    // prefer spl_object_hash if available
    if ( $spl_function_exists )
        return spl_object_hash( $object );

    // validate input
    if ( !is_object( $object ) ) { 
        trigger_error( __FUNCTION__ . '() expects parameter 1 to be object', E_USER_WARNING );
        return null;
    }
    // setup prefix and counter to generate object hash, set it to object if not set
    isset( $prefix ) || ( ( $prefix = uniqid( '' ) ) && $property .= $prefix . '__' );
    isset( $object->$property ) || ( $object->$property = sprintf( '%s-%08d', $prefix , ++$count ) );
    return $object->$property;
}

如果您使用的是 PHP 5 版本,则不需要通过引用传递参数。

于 2011-11-08T17:02:16.010 回答
1

这就是你想要的。

我已经修复了一个非常可能的错误并将函数从bobthecow 答案(也是从 php.net 借来的)简化为:

if ( !function_exists( 'spl_object_hash' ) ) {
    function spl_object_hash( $object )
    {
        ob_start();
        var_dump( $object );
        preg_match( '[#(\d+)]', ob_get_clean(), $match );
        return $match[1];
    }
}

它返回一个整数(通常在 100 以下范围内),对于任何对象都是唯一的(有关您所看到的详细信息,请参阅此答案)。


PS我在现实世界的场景中使用这个实现

于 2013-10-08T14:38:15.217 回答
0

uniqid()会为您的任务工作吗?

于 2010-02-23T03:48:35.830 回答