Hudzilla.org - the homepage of Paul Hudson
Contents > Objects Wish List | Report Bug | About Me ]

6.2     Classes

This is NOT the latest copy of this book; click here for the latest version.

The blueprints of dog breeds and animals are known as classes - they define the basic architecture of the objects available in our programs. Each class is defined as having a set of functions and variables, and you can inherit one class from another - our Breed classes, for example, inherited from the Dog class, thereby getting all the Dog functions and variables available. Inheriting is often referred to a sub-classing - "poodle" would be a sub-class of "dog".

Some languages, such as C++, allow you to inherit from more than one class, which is known as multiple inheritance. This technique allows you to have a class bird and a class horse, then create a new class called "Flying Horse", which inherits from both bird and horse, to give you animals like the mythical Pegasus. PHP does not allow you to do this because it generally makes for very confusing programs, and is quite rare even in C++.

PHP allows you to inherit from precisely one parent class, and you can inherit as many times as you want. For example, the dog class could inherit from the class Carnivora, which would contain cats, dogs, bears, etc. Carnivora could inherit from Mammalia, which holds all mammals, which could in turn inherit from Vertebrata, holding all animals with a backbone, etc - the higher up you go, the more vague the classes become. This is because each class inherits the functions and variables from its parent class, as well as adding its own.

Author's Note: people often use the terms "parent", "child", "grandparent", etc, to define their class structure. A child class is one that inherits from another - "poodle" is a child of "dog", and would be a grandchild of "carnivora". "Carnivora" would be the parent of "dog" and grandparent of "poodle" - this will make more sense later, when you are creating your own classes and sub-classing freely.





<< 6.1 Conceptual overview   6.2.1 Defining a class >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 07 Sep 2008

An object is, in effect, an "instance" of a class.

Let's say you wanted to keep track of 3 apples, and had a class named "Apple." In this case--apple1, apple2, apple3, are all objects of the Apple class.

Does this make sense?

A PHP User - 07 Sep 2008

so whats the difference between an object and a class?
im guessing a class is an object, so does that mean there are other things that can be objects too?



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is nine plus six?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow