PHP
4 Cevap php

4 Cevap

Ben hızlı testlerin bir çift koştu. Ben gerçekten bind('evt_name', array($obj, 'callback_function')) kullanarak bind () fonksiyonu gerçek geri aramaları depolamak daha iyi olacağını düşünüyorum. Kesinlikle yerine olay bağlamaları ile referanslar depolamak yerine, spl_object_hash rota gitmek isterseniz, böyle bir şey bakıyoruz:

A var_dump / extract and hash id implementation:

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;
}

A naive references implementation:

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;
}

Bu bir yönüyle sınıf-tabanlı referans işlevi daha temelde 5-50x kötüydü, bu yüzden endişe değmez.

A store references by class implementation:

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;
}

Ve results that look like this ile sonuna kadar. Özet: - 1/3 var_dump tabanlı uygulama performansını koparmak için yönetir, en iyi, ve genellikle {[(2) Neler sınıf temelli referanslar uygulaması iyi çevresinde N/50 sınıflar gerçekleştirir ]} kötü.

var_dump uygulanması ideal değil ama, tolere edilebilir gibi görünüyor. Eğer bu sorgulardan çok yapmıyorsunuz Ama eğer sizin için bir darboğaz olmayacak. Özellikle PHP

Ben bir kez, nesne başına bir benzersiz bir karma sunan bir sayaç ile çalışır ve bir nesneye atanmışsa bir kamu sınıf özelliği olarak başına karma depolayan wordpress için bir yardımcı işlev yazdı. Aşağıdaki örnek bunu göstermektedir:

/**
 * 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;
}

Eğer bir PHP 5 sürümünü kullanıyorsanız, size referans ile parametre geçmek gerekmez.

uniqid() görev için çalışmak istiyorsunuz?

This is what you want.

Ben çok olası bir hata düzeltildi ve bu bobthecow answer (ayrıca php.net ödünç olan) gelen işlevi aerodinamik ettik:

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];
    }
}

Bu herhangi bir nesne için benzersiz bir tamsayı (genellikle alt-100 aralığında), (this answer ne görüyorsanız ilgili ayrıntılar için bakınız) döndürür.


P.S. Ben gerçek bir dünya senaryosunda bu uygulamasını kullanabilirsiniz here