Objects within objects

Just like any other variable, objects can be used within other objects and are similarly accessed by using the small arrow -> access operator again.


This example instantiates a new (Display) object on line 23 that is assigned to the team property within the Rider object, it then assigns a value to the Display object's output property on line 27, and finally echo it back on line 30 to show the assignment has taken place:

Save & refresh browser:

An object instance of the Rider class has been instantiated.
An object instance of the Display class has been instantiated.
My name is Marc Marquez and I currently ride for Honda.


*note: the use of constructors, on lines 5 & 14,  is explained under the constructor magic method here

Leave a Reply