Skip to Main Content
PHP in a Nutshell
book

PHP in a Nutshell

by Paul Hudson
October 2005
Intermediate to advanced content levelIntermediate to advanced
372 pages
11h 35m
English
O'Reilly Media, Inc.
Content preview from PHP in a Nutshell

Objects Within Objects

You can use objects inside other objects in the same way as other variable types. For example, we could define a DogTag class and give each Dog a DogTag object like this:

    class DogTag {
            public $Words;
    }

    class Dog {
            public $Name;
            public $DogTag;

            public function bark() {
                    print "Woof!\n";
            }
    }

    // definition of Poodle...

Accessing objects within objects is as simple as using -> again:

    $poppy = new Poodle;
    $poppy->Name = "Poppy";
    $poppy->DogTag = new DogTag;
    $poppy->DogTag->Words = "My name is Poppy. If you find me, please call 555-
    1234";

The $DogTag property is declared like any other, but needs to be created with new once $poppy has been created.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP Cookbook

PHP Cookbook

Eric A. Mann
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe
Learning PHP

Learning PHP

David Sklar

Publisher Resources

ISBN: 0596100671Errata Page