Accessing Properties and Methods

The properties of an instance are variables, just like any other PHP variable. To refer to them, however, you must use the -> operator. You do not use a dollar sign in front of the property name. For an example, refer to the line in Listing 6.1 that prints the name property of the currentUser object.

Use of -> can be chained. If an object's property contains an object itself, you can use two -> operators to get to a property on the inner object. The parser in PHP 3 was unable to deal with complex expressions like this. In PHP 4 you are not limited this way. You may even place these expressions within double-quoted strings. See Listing 6.2 for an example of an object that contains an array of objects.

Unlike object-oriented ...

Get Core PHP Programming: Using PHP to Build Dynamic Web Sites now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.