0

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

2 Answers 2

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

1
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

1
  • 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) : php.net/manual/en/language.oop5.references.php#101900
    – Oberst
    Mar 8, 2016 at 17:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .