PHP < 5.2 の spl_object_hash (オブジェクト インスタンスの一意の ID)
質問
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 など) では機能しません。2 番目は次のとおりです。
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;
}
それは大きなパフォーマンスバスターになる可能性があるようです!
誰か何か考えている人はいますか?
解決
いくつかの簡単なテストを実行しました。実際のコールバックをbind()関数に保存する方が良いと思います。 bind('evt_name', array($obj, 'callback_function'))
. 。イベント バインディングで参照を保存するのではなく、絶対に spl_object_hash ルートに進みたい場合は、次のようになります。
var_dump / 抽出およびハッシュ 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 クラスあたりで最もパフォーマンスが高くなります。最高の場合でも、クラスベースの参照実装のパフォーマンスの 1/3 を引き出すことができます。 var_dump
ベースの実装であり、通常は 多くの 悪い。
の var_dump
理想的ではありませんが、実装は許容できるようです。ただし、これらの検索をあまり頻繁に行わない場合は、ボトルネックにはなりません。特に PHP 5.2 未満のボックスのフォールバックとして。
他のヒント
私は一度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のバージョンを使用している場合は、参照によってパラメータを渡す必要はありません。
これがあなたが望むものです。
非常に可能性の高いバグを修正し、機能を合理化しました。 ボブザカウの答え (これも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 未満の範囲) を返します (「 この答え 表示されている内容の詳細については、こちらをご覧ください)。
追伸この実装を現実のシナリオで使用します ここ
uniqid()の仕事?