For the sake of completeness in talking about PHP debugging, I thought I'd go over some other techniques of getting class information by using a print_r function
To do this, I've built a simple PersonLocation class with a parent Person class to use as an example. Here it is:
Let's point out some things before we start. The parent class is linked to the ParentLocation class with the "extends" keyword. The parent class gives the person's first and last name, which we have chosen to make a protected and private property to show you how these are outputted, while in the class PersonLocation the city and state properties are public. For this example, we have chosen to give the properties default values, which we would not do normally, or if we did, we would make them empty strings, otherwise every object we instantiate would be for one person, not very useful. We instantiate the class PersonLocation with "new," and call the object, $robloc. For the sake of neatness, when we show the results, we've surrounded our print_r statement with pre tags to make the output line up vertically.
Let's get started. First we'll run this application. Here's the outcome.
We are informed of the protected and private properties of our parent class, along with the fact, that the variable, $robloc, is a PersonLocation object.
Suppose we are coding along and forget the properties of a class. We can use a function with the print_r statement, in this case, we'll use "get_class_vars." First, let's get the variables for class "PersonLocation." Here's the print_r code:
We commented out the object. We do not have to instantiate an object to get our variables with "get_class_vars." Notice also that no $ sign is needed with the print_r statement to get the class vars. Here's the output:
What happen? The protected and private properties are not shown when you use get_class_vars. Hmmm, how can we uncover all the variables in the class? We could instantiate an object and use print_r on the object, as we did in the first example, but let's look at another way.
Along with get_class_vars there is another PHP function called get_class_methods. Let's start with the Parent class. The query is:
and the output is:
It is highly unusual to write protected or private properties without the methods to get and set them.
With get_class_methods we have a way, when we are coding, to quickly get all the methods avaiable for a particular class. What if we're down in the weeds in a sub class and want our methods. Let's see the output for print_r(get_class_methods('PersonLocation')).
To wrap up this post, what if we wanted to know all the classes that are defined, or available to us, at that moment in the code. There is a function we can use with print_r called, "get_declared_classes." The query looks like this:
And the output may surprise you. I've taken out the pre's to cut down on the length of the output.
These are all the classes that the PHP language gives you as part of your code that are available to you. The very first class mentioned is the Exception class which we use for error handling with the try and catch syntax. Error handling is a good topic for future posts, when we'll use the exception class methods. If you'd like to explore them now, you certainly can with the print_r (get_class_methods('Exception')). Give it a try. By the way, you'll notice at the very end of the output is the Person and PersonLocation classes we have been playing with through out this post.