How to see memory address of an object in Xdebug / PHPStorm

0

1

Is there any way to see the allocated memory address of a variable, object when debugging with Xdebug and PHPStorm ?

Don't know if this is a setting within Xdebug or PHPstorm, but I'm sure is possible Now it just shows the value not the address..

My goal is to see if I'm really using the same instance of an object in some other class

PartySoft

Posted 2015-08-31T20:19:03.740

Reputation: 101

Answers

1

Im sure you already figured this out by now, but incase you havent (and for the sake of providing a lonely question with an answer)

(this assumes you already have xdebug working with PHPStorm, and are able to debug through PHPStorm)
In the current version of PHPStorm (10.0 as of the time of this writting), in the debug window, there is a yellow circle with an "@" in it. Clicking that will show you the memory addresses.

In my examples below, notice my $ds object. It just lists the class (DBConnection). After turning on the addresses, you can see them. (DBConnection@69796600)

This button enter image description here

Oberst

Posted 2015-08-31T20:19:03.740

Reputation: 203

Apparently modern xdebug (2.4.0+) removed the ability to see memory addresses https://youtrack.jetbrains.com/issue/WI-33581

– allejo – 2019-12-03T07:27:26.417

1

Don't make the same error as me. The address of the object may be different while the object IS the same (i.e spl_object_hash returns the same id for both objects).

For instance, I wrote this test:

$a = $b = new StdClass;
$objects = [$a, $b];

var_dump(spl_object_hash($a), spl_object_hash($b));

foreach ($objects as $object1);

var_dump(spl_object_hash($a), spl_object_hash($b));

foreach ($objects as &$object2);

var_dump(spl_object_hash($a), spl_object_hash($b));

As expected, the id returned by spl_object_hash() is the same of $a, $b and the objects in $objects. But the address of the objects in $objects are not the same as $a and $b:

string(32) "000000007fbf1856000000002722d91d"
string(32) "000000007fbf1856000000002722d91d"
string(32) "000000007fbf1856000000002722d91d"
string(32) "000000007fbf1856000000002722d91d"
string(32) "000000007fbf1856000000002722d91d"
string(32) "000000007fbf1856000000002722d91d"

enter image description here

Zef

Posted 2015-08-31T20:19:03.740

Reputation: 11

Well, of course your example changed the memory address. Accessing an object by reference causes the pointer to use the address of the new variable. You can see this in action by another simple test (separate each onto new lines... Comments are stupid). $a=new StdClass;$b=$a;$c=$b;echo '';$d=&$a;echo '';$e=&$b;echo ''; Add a breakpoint to each echo and you can watch the addresses change between each assignment. Ill refer you a nice comment on pointers and references on the PHP site (specifically the 3rd principle) : http://php.net/manual/en/language.oop5.references.php#101900

– Oberst – 2016-03-08T17:58:37.573