How to Inspect an Object in PHP 


               


How to Inspect an Object in PHP

php


In this article, we go over how to inspect an object in PHP.

If we ever want to check all of the parameters of an object, you can use the PHP var_dump function.

This lets you see everything in an object, all the properties that have been assigned to the object.

The PHP var_dump() function is very similar to the PHP print_r() function, which lets you see the internal composition of an array.

So below I give code on how to check an object we create called $car1. It represents a car object of the cars class. We then inspect this $car1 object using the var_dump() function.



So in the code above, we create a class named cars.

In this class, we declare 4 property variables, $type, $year, $engine, and $energy.

We then create an object of this cars class called $car1.

We assign this $car1 object its type, year, engine type, and energy type.

We then echo out the object using the var_dump() function.

We use the <pre></pre> tags just to make the code readable. These tags separate the data into separate lines, making the code more readable.

Running the PHP code yields the following output below.

Actual PHP Output


object(cars)#1 (4) {
  ["type"]=>
  string(13) "Tesla Model 3"
  ["year"]=>
  int(2017)
  ["engine"]=>
  string(9) "Automatic"
  ["energy"]=>
  string(8) "Electric"
}



So you can see the output above.

It tells you the name of the object, what index it is for the objects of a class, and how many characters the name of the object is composed of. It then tells you each property that makes up an object and the value of each of the properties. For each value, it tells you what data type it is and how many characters make up the value.

So the PHP var_dump function is very good for inspecting an object and, thus, can be used for troubleshooting an object. It really tells you all there is to know about an object.

So this is how you can inspect an object in PHP.


Related Resources

How to Instantiate an Object in PHP

How to Delete an Object in PHP

How to Initialize an Object in PHP

How to Invoke a Method On an Object in PHP

How to Clone an Object in PHP

How to Count the Number of Objects in a Class in PHP




HTML Comment Box is loading comments...