6.6. 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 line in Listing 6.1 that prints the name property of the user 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. You may even place these expressions within double-quoted strings. See Listing 6.5 for an example of an object that contains an array of objects.

Accessing methods is similar to accessing properties. The -> operator is used to point to the instance's method. This is shown in ...

Get Core PHP Programming, Third Edition 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.