We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

About dependency injector and var_dump

When passing an object that extends PhalconModel to var_dump(), the output will show all the object properties plus what looks like my Dependency Injector (!!!). However, there is a ->dump() function in all objects that extend Phalcon\Model that can be used with var_dump() to output a clean version of the object.

How do I output a clean version of the objects contained in an array?

$myArray = [...bunch of objects...];
var_dump($myArray);

The output from the code above will show all objects plus a dump of the Dependency Injector for each one of them, making it impossible to analyze/debug the array contents. And if one of those objects contains another object as one of its properties the rabbit hole will only get deeper.

What's the best way to debug?



145.0k
Accepted
answer
array_walk($myArray, function($item) { var_dump($item->toArray()); });

Thanks for your reply.

Any suggestion for this other case:

class B extends Phalcon\Mvc\Model{
   public $name;
}
class A extends Phalcon\Mvc\Model{
   public $name;
   public $bs;
}

$b1 = new B();
$b2 = new B();

$a1 = new A();
$a1->bs = [$b1, $b2];

var_dump($a1->dump()); //All B instances inside $bs will be dumped with their dependencies.
array_walk($myArray, function($item) { var_dump($item->toArray()); });
edited Oct '16
array_walk($a1->bs, function($item) { var_dump($item->toArray()); });
var_dump($a1);

Or just use xdebug

edited Oct '16

Let me try to explain better

Scenario 1

Dumping a simple object => var_dump($x->dump())

Works great

Scenario 2:

Dumping an array os objects => array_walk()

Works great

Scenario 3:

Dumping an object that contains an array of objects:

$a->name = 'zeta';

$a->myArray = [$obj1, $obj2, $obj3, ...];

How to dump this?

var_dump($a) will be a complete mess

var_dump($a->dump()) will strip the DI dump from the object, but will keep it on the objects contained in the array

array_walk($a->myArray) will strip the DI dump from the objects contained in the array, but will not show the informations of the parent object ($a)

None of those solutions get close to the result one would get using vanilla objects.

What dump configs are you using on your xdebug?

array_walk($a1->bs, function($item) { var_dump($item->toArray()); });
var_dump($a1);

Or just use xdebug

edited Oct '16

Try with this nice tool to get the visual over your objects: https://raveren.github.io/kint/

In general theory, var_dump() is not the way to do it with OOP in any case. You need to know your objects, to have some interface or public methods/properties to work with etc. What would you do in Java? Dump bytecode? No, you'll read description of the class you need to work with and use only what's in your accessibile scope.

edited Oct '16

On xdebug you are not doing dump, just setting breakpoint in IDE and checking in IDE values of properties :)

Also as many this is not phalcon issue. I would recommend to post this on some php forum to figure out the best approach but it will be xdebug.

xdebug is still a 3rd party tool to help you out. This kint tool at least does not need to be installed as binary extension, and it helps to find your way through objects.