如何在 PHP 中查找对象 ID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/308133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find Object ID in PHP?
提问by Joel
I'm using PHP 5.2. I'd like to find a way to output a unique id for every object, so it's easy when looking over logs to see which objects are the same.
我正在使用 PHP 5.2。我想找到一种为每个对象输出唯一 ID 的方法,因此在查看日志以查看哪些对象相同时很容易。
In Ruby, I'd just say object.object_id to get Ruby's internal identifier for the object. There doesn't seem to be an obvious way to do this in PHP.
在 Ruby 中,我只是说 object.object_id 来获取对象的 Ruby 内部标识符。在 PHP 中似乎没有明显的方法可以做到这一点。
Is there is a built-in way of doing this? If there isn't, can you offer any other suggestions?
有没有内置的方法来做到这一点?如果没有,你能提供任何其他建议吗?
回答by azkotoki
Use spl_object_hash()for that.
spl_object_hash()为此使用。
It returns an unique identifier for each object instance, and not the name of the class, so it seems more suitable for you.
它为每个对象实例返回一个唯一标识符,而不是类的名称,因此它似乎更适合您。
Edit:
编辑:
For PHP < 5.2.x users, see this answer.
对于 PHP < 5.2.x 用户,请参阅此答案。
回答by mindplay.dk
There is currently no way to do this in PHP, as of version 5.3.6.
从版本 5.3.6 开始,目前无法在 PHP 中执行此操作。
spl_object_hash() does not do what you want - because it recycles the identifiers when objects get deleted, this will lead to errors in (for example) an object-relational mapper trying to keep track of objects in a session.
spl_object_hash() 不会做你想做的 - 因为它在对象被删除时回收标识符,这将导致(例如)对象关系映射器试图跟踪会话中的对象时出错。
The description at the top of the documentation page ("This function returns a unique identifier for the object. This id can be used as a hash key for storing objects or for identifying an object.") is wrong - the truth is revealed in the note on that page: "When an object is destroyed, its hash may be reused for other objects", or in other words, the function does notalways return a unique identifier, and can notalways be used for storing or identifying objects.
文档页面顶部的描述(“此函数返回对象的唯一标识符。此 id 可用作存储对象或识别对象的哈希键。”)是错误的 - 真相在注意,页:“当一个对象被销毁,其散列可以重新用于其它目的”,或者换句话说,该函数不会不总是返回一个唯一的标识符,并且可以不总是被用于存储或识别对象。
The technique demonstrated in this commentmay work in some cases, but it not reliable and will not work consistently either, since attempting to access an undefined property will invoke the __get() and __set() magic methods, the results of which are unpredictable.
此注释中演示的技术可能在某些情况下有效,但它不可靠,也不会始终如一地工作,因为尝试访问未定义的属性将调用 __get() 和 __set() 魔术方法,其结果是不可预测的。
In conclusion, the short answer to your question (unfortunately) is "no" - there is no such method in PHP, and there is no way to write a method like this that will work consistently for any object.
总之,对您的问题的简短回答(不幸的是)是“不”-PHP 中没有这样的方法,并且无法编写这样的方法来对任何对象始终如一地工作。
If you would like to see this feature added to PHP, please vote and/or comment here:
如果您希望看到此功能添加到 PHP,请在此处投票和/或评论:
回答by i336_
?? PHP 7.2.0 introduces spl_object_id()!
?? PHP 7.2.0 引入了spl_object_id()!
$test = (object)[];
var_dump(spl_object_id($test)); # int(1)
Caveat emptor(?)警告清空者(?):
When an object is destroyed, its id may be reused for other objects.
当一个对象被销毁时,它的 id 可能会被其他对象重用。
回答by rolacja
I know this is old topic, but i think i've found a solution.
我知道这是个老话题,但我想我已经找到了解决方案。
The trick is in storing reference to each object in array with assigned key. You can then get object id by searching through that array and returning found key.
诀窍是使用分配的键存储对数组中每个对象的引用。然后,您可以通过搜索该数组并返回找到的键来获取对象 ID。
<?php
class objectMarker
{
private $storage;
function add($object) {
$this->storage[] = $object;
}
function getId($object) {
foreach ($this->storage as $id => $item) {
if ($item === $object) {
return $id;
}
}
return null;
}
}
$marker = new objectMarker;
$t1 = new stdClass;
$t2 = new stdClass;
$marker->add($t1);
$marker->add($t2);
echo $marker->getId($t1) . "\n";
echo $marker->getId($t2) . "\n";
unset($t1);
$t1 = new stdClass;
$marker->add($t1);
echo $marker->getId($t1) . "\n";
$t2->x = 1;
echo $marker->getId($t2) . "\n";
/* output:
0
1
2
1
*/

