FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » object id - where?
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
object id - where? [message #177902] Thu, 26 April 2012 09:34 Go to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Hi,

is there something like an object id accessible?

Example: I do this

php > $a = new stdclass;
php > $a->b = new stdclass;
php > var_dump($a);
object(stdClass)#1 (1) {
["b"]=>
object(stdClass)#2 (0) {
}
}
php >

and var_dump() shows me unique ids of the classes. Is there a function for it?

/Str.
Re: object id - where? [message #177904 is a reply to message #177902] Thu, 26 April 2012 09:38 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 4/26/2012 5:34 AM, M. Strobel wrote:
> Hi,
>
> is there something like an object id accessible?
>
> Example: I do this
>
> php> $a = new stdclass;
> php> $a->b = new stdclass;
> php> var_dump($a);
> object(stdClass)#1 (1) {
> ["b"]=>
> object(stdClass)#2 (0) {
> }
> }
> php>
>
> and var_dump() shows me unique ids of the classes. Is there a function for it?
>
> /Str.

Classes do not have id's. Objects have unique ids, but they are only
for reference and can change.

Why would you want such a function?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: object id - where? [message #177906 is a reply to message #177904] Thu, 26 April 2012 09:42 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 26.04.2012 11:38, schrieb Jerry Stuckle:
> On 4/26/2012 5:34 AM, M. Strobel wrote:
>> Hi,
>>
>> is there something like an object id accessible?
>>
>> Example: I do this
>>
>> php> $a = new stdclass;
>> php> $a->b = new stdclass;
>> php> var_dump($a);
>> object(stdClass)#1 (1) {
>> ["b"]=>
>> object(stdClass)#2 (0) {
>> }
>> }
>> php>
>>
>> and var_dump() shows me unique ids of the classes. Is there a function for it?
>>
>> /Str.
>
> Classes do not have id's. Objects have unique ids, but they are only for reference
> and can change.
>
> Why would you want such a function?
>

To detect loops in a tree. Just an idea.

I know I can give them an id, but only if it's necessary...

/Str.
Re: object id - where? [message #177914 is a reply to message #177906] Fri, 27 April 2012 00:33 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 4/26/2012 5:42 AM, M. Strobel wrote:
> Am 26.04.2012 11:38, schrieb Jerry Stuckle:
>> On 4/26/2012 5:34 AM, M. Strobel wrote:
>>> Hi,
>>>
>>> is there something like an object id accessible?
>>>
>>> Example: I do this
>>>
>>> php> $a = new stdclass;
>>> php> $a->b = new stdclass;
>>> php> var_dump($a);
>>> object(stdClass)#1 (1) {
>>> ["b"]=>
>>> object(stdClass)#2 (0) {
>>> }
>>> }
>>> php>
>>>
>>> and var_dump() shows me unique ids of the classes. Is there a function for it?
>>>
>>> /Str.
>>
>> Classes do not have id's. Objects have unique ids, but they are only for reference
>> and can change.
>>
>> Why would you want such a function?
>>
>
> To detect loops in a tree. Just an idea.
>
> I know I can give them an id, but only if it's necessary...
>
> /Str.

I'm not sure the object id remains consistent. I don't see any reason
it would change, but I've never seen anything in PHP indicating it has
to be that way, either.

I'd suggest just sticking your own id in the object. Then you know what
you have.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: object id - where? [message #177930 is a reply to message #177902] Fri, 27 April 2012 20:07 Go to previous messageGo to next message
Thomas Mlynarczyk is currently offline  Thomas Mlynarczyk
Messages: 131
Registered: September 2010
Karma: 0
Senior Member
M. Strobel schrieb:

> is there something like an object id accessible?

You can use === to check if two variables refer to the same object:

$a = new stdclass;
$a->foo = 42;
$b = new stdclass;
$b->foo = 42;
$c = $a;
var_dump( $a === $b, $a === $c, $b === $c );

Result:
false, true, false

Greetings,
Thomas

--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
Re: object id - where? [message #177956 is a reply to message #177902] Mon, 30 April 2012 11:19 Go to previous messageGo to next message
Goran is currently offline  Goran
Messages: 38
Registered: January 2011
Karma: 0
Member
On 26.4.2012 11:34, M. Strobel wrote:
> is there something like an object id accessible?

Try spl_object_hash()

http://www.php.net/manual/en/function.spl-object-hash.php
Re: object id - where? [message #177957 is a reply to message #177956] Mon, 30 April 2012 12:43 Go to previous message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 30.04.2012 13:19, schrieb Goran:
> On 26.4.2012 11:34, M. Strobel wrote:
>> is there something like an object id accessible?
>
> Try spl_object_hash()
>
> http://www.php.net/manual/en/function.spl-object-hash.php

I see. One of the 1597 internal functions in my system...

/Str.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Stats comp.lang.php (last 7 days)
Next Topic: Webhosting, Domain Name, Data Center Cyprus, Hosting Services
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Nov 22 03:07:28 GMT 2024

Total time taken to generate the page: 0.02642 seconds