Section 8.2. Classes


8.2. Classes

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 methods and properties, and you can inherit one class from anotherour Breed classes, for example, inherited from the Dog class, thereby getting all the Dog methods and properties available. Inheriting is often referred to as subclassingPoodle would be a subclass 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 FlyingHorsewhich inherits from both Bird and Horseto 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 Cat, Dog, Bear, etc. Carnivora could inherit from Mammalia, holding 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 methods and properties from its parent class, as well as adding its own.

People often use the terms parent, child, grandparent, etc., to define their class structure. A child class is one that inherits from anotherPoodle is a child of Dog, and would be a grandchild of Carnivora. Carnivora would be the parent of Dog and grandparent of Poodlethis will make more sense later, when you are creating your own classes and sub-classing freely.


8.2.1. Defining a Class

Given the class structure of dogs and breeds discussed above, it is time to take a look at how that translates into PHP code. Here is the PHP code necessary to define a very basic Dog class:

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

Here the Dog class has just one method, bark( ), which outputs "Woof!". Don't worry about the public part for nowthat just means "can be called by anyone" and we'll be looking at that later. If we create an object of type Dog, we could call its bark( ) method to have it output the message.

Class naming conventions follow the same rules as variable naming, excluding the dollar sign at the beginning. You can use any name for your methods, except stdClass and _ _PHP_Incomplete_Classboth of these are reserved by PHP.


8.2.2. How to Design Your Class

When designing your classes, there is one golden rule: keep to real-world thinking. However, although that one rule sounds simple, it's nebulouswhat exactly is real-world thinking? Fortunately there are a number of more simple rules you can follow that will help keep your code particularly readable:

  • Start or end local properties with a special character, so that you are always clear about what variable is being set. The most common method is to start local properties with an underscore, e.g., _Name, _Age, etc.

  • To follow OOP guidelines strictly, nearly all of your properties should be either private or protectedthey should not be accessible from outside of an object. More on this later.

  • Write accessor methods to set and get private properties. These methods should be how you interface with the object. To get a property called _Age, write a method Age( ). To set a property called _Age, write a method SetAge( ).

  • Always put properties and methods as low in your inheritance as they can go without repetition. If you find one object has properties and methods it is not supposed to have, you have gone wrong somewhere. For example, while dolphins can swim, gorillas cannot, so do not put a swim( ) method into a Mammal class just to save time.

If you are wondering why it is that accessor methods should be used to read and write properties, it is because OOP practice dictates that objects should be self-contained. That is, other parts of your program should be able to work with them using simple method calls, so that they do not need implicit knowledge of an object's internal structures and operations.

8.2.3. Basic Inheritance

To extend the Dog class to breeds, the extends keyword is needed, like this:

     class Dog {             public function bark( ) {                     print "Woof!\n";             }     }     class Poodle extends Dog {             // nothing new yet     }

8.2.4. Overriding Methods

PHP allows us to redefine methods in subclasses, which means we can make the Poodle class have its own version of bark( ). This is done by redefining the method inside the child class, making the Poodle class look like this:

     class Poodle extends Dog {             public function bark( ) {                     print "Yip!\n";             }     } 

We'll come back to inheritance after we look at objects actual instances of our classes.

8.2.5. The Scope Resolution Operator

The scope resolution operator is ::two colons next to each other. It is used in object-oriented programming when you want to access static or overridden methods of a class. For example, if you have a method sayhello( ) as well as a sayhello( ) method of a Person object, you would use Person::sayhello( )you resolve which sayhello( ) you mean by using the class name and the scope resolution operator.

The most common use for scope resolution is with the pseudo-class parent. For example, if you want a child object to call its parent's _ _construct( ) method, you would use parent::_ _construct( ). This is shown later in this chapter, in the section "Parent Constructors."

Internally to PHP, the scope resolution operator is called "paamayim nekudotayim," which is Hebrew for "double colon."




PHP in a Nutshell
Ubuntu Unleashed
ISBN: 596100671
EAN: 2147483647
Year: 2003
Pages: 249

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net