获取PHP中对象的实例ID [英] Get Instance ID of an Object in PHP
问题描述
我之前在StackOverflow上了解到,我们可以获得实例ID,例如:
var_dump(intval(curl_init())); // int(2)
var_dump(intval(finfo_open())); // int(3)
var_dump(intval(curl_init())); // int(4)
var_dump(intval(finfo_open())); // int(5)
var_dump(intval(curl_init())); // int(6)
我需要类似的东西,但适用于类:
class foo {
public function __construct(){
ob_start();
var_dump($ this); // object(foo)#INSTANCE_ID(0){}
echo preg_replace('〜。+#(\d +)。+〜s','$ 1',ob_get_clean());
}
}
$ foo = new foo(); // 1
$ foo2 = new foo(); // 2
上面的工作,但我希望一个更快的解决方案,没有涉及输出缓冲区。请注意,这不一定在构造函数中使用,甚至不会在类本身内使用。
spl_object_hash c $ c>不是我正在寻找的,因为两个对象产生相同的散列
问题以前包含一个不正确的例子 spl_object_hash
输出;确保两个对象同时存在产生微妙不同的散列:
var_dump(spl_object_hash($ foo)); // 0000000079e5f3b60000000042b31773
var_dump(spl_object_hash($ foo2)); // 0000000079e5f3b50000000042b31773
对于对象来说,转换为int似乎不起作用:
注意:类foo的对象无法转换为int。
有没有使用对象属性获取相同输出的快速方法?
code> var_dump(),我通过试验和错误发现 debug_zval_dump()
也输出对象实例,不幸的是,它也需要输出缓冲,因为它不返回结果。
spl_object_hash()
可以帮助你在这里。
返回该对象的唯一标识符
对于给定的实例,它总是相同的。
EDIT $ b
您可以使用静态类属性实现这样的行为,例如:
class MyClass
{
private static $ _initialized = false;
public function __construct()
{
if(!self :: $ _ initialized){
self :: $ _ initialized = true;
//您的只运行代码
}
}
}
但实际上这与你原来的问题没有关系。
I've learn a while ago on StackOverflow that we can get the "instance ID" of any resource, for instance:
var_dump(intval(curl_init())); // int(2)
var_dump(intval(finfo_open())); // int(3)
var_dump(intval(curl_init())); // int(4)
var_dump(intval(finfo_open())); // int(5)
var_dump(intval(curl_init())); // int(6)
I need something similar but applied to classes:
class foo {
public function __construct() {
ob_start();
var_dump($this); // object(foo)#INSTANCE_ID (0) { }
echo preg_replace('~.+#(\d+).+~s', '$1', ob_get_clean());
}
}
$foo = new foo(); // 1
$foo2 = new foo(); // 2
The above works but I was hoping for a faster solution or, at least, one that didn't involve output buffers. Please note that this won't necessarily be used within the constructor or even inside the class itself!
spl_object_hash()
is not what I'm looking for because the two objects produce identical hashes
The question previously contained an incorrect example of spl_object_hash
output; ensuring that both objects exist at the same time produces hashes which are subtly different:
var_dump(spl_object_hash($foo)); // 0000000079e5f3b60000000042b31773
var_dump(spl_object_hash($foo2)); // 0000000079e5f3b50000000042b31773
Casting to int like resources doesn't seem to work for objects:
Notice: Object of class foo could not be converted to int.
Is there a quick way to grab the same output without using object properties?
Besides var_dump()
, I've discovered by trial and error that debug_zval_dump()
also outputs the object instance, unfortunately it also needs output buffering since it doesn't return the result.
spl_object_hash()
could help you out here. It
returns a unique identifier for the object
which is always the same for a given instance.
EDIT after OP comment:
You could implement such a behavior using a static class property, e.g:
class MyClass
{
private static $_initialized = false;
public function __construct()
{
if (!self::$_initialized) {
self::$_initialized = true;
// your run-only-once code
}
}
}
But actually this has nothing to with your original question.
这篇关于获取PHP中对象的实例ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!