Object Inheritance

Object Inheritance

Having learned the absolute basics of objects, properties, and methods, you can start to look at object inheritance. Inheritance with regard to classes is just what it sounds like: One class inherits the functionality from its parent class. Listing 9.7 shows an example.

ارایه درگاه پرداخت ePayBank.ir برای برنامه نویسان و وب مستران سایتهای ایرانی و وبلاگها

درج تبلیغات اینترنتی در MyCityAd.ir رایگان هست. هم اکنون خدمات خود را درج آگهی کنید.

آموزش برنامه نویسی طراحی اختصاصی سایت در mortezasaheb.ir امکانپذیر هست.

Listing 9.7. A Class Inheriting from Its Parent

1:  <?php
2:  class myClass {
3:     var $name = "Matt";
4:     function myClass($n) {
5:          $this->name = $n;
6:     }
7:     function sayHello() {
8:          echo "HELLO! My name is ".$this->name;
9:     }
10: }
11: class childClass extends myClass {
12: //code goes here
13: }
14: $object1 = new childClass("Baby Matt");
15: $object1 -> sayHello();
16: ?>

If you save this code as inheritance.php, place it in your document root, and access it with your web browser, you will see the following on your screen:

HELLO! My name is Baby Matt

Lines 46 make up a constructor. Notice that this function is named the same as the class in which it is contained: myClass. Lines 1113 define a second class, childClass, that contains no code. That’s fine because, in this example, it’s only meant to demonstrate inheritance from the parent class. The inheritance occurs through the use of the extends clause, shown in line 11. The second class inherits the elements of the first class because this clause is used.

One last example shows you how a child class can override the methods of the parent classsee Listing 9.8.

Listing 9.8. The Method of a Child Class Overriding That of Its Parent

1: <?php
2: class myClass {
3:    var $name = "Morteza Saheb";
4:    function myClass($n) {
5:         $this->name = $n;
6:    }
7:    function sayHello() {
8:         echo "HELLO! My name is ".$this->name;
9:    }
10: }
11: class childClass extends myClass {
12:     function sayHello() {
13:          echo "I will not tell you my name.";
14:     }
15: }
16: $object1 = new childClass("AriaCoders");
17: $object1 -> sayHello();
18: ?>

The only changes in this code from Listing 9.7 are the new lines 1214. In these lines, a function is created called sayHello() that, instead of printing HELLO! My name is..., prints the message I will not tell you my name. Now, because the sayHello() function exists in childClass, and childClass is the class that is called in line 16, its version of sayHello() is the one used.

If you save this code as inheritance2.php, place it in your document root, and access it with your web browser, you will see the following on your screen:

I will not tell you my name.

Like most elements of object-oriented programming, inheritance is useful when attempting to make your code flexible. Suppose you created a text-formatting class that organized and stored data, formatted it in HTML, and output the result to a browseryour own personal masterpiece. Now suppose you had a client who wanted to use that concept, but instead of formatting the content into HTML and sending it to a browser, he wanted to format it for plain text and save it to a text file. No problemyou just add a few methods and properties, and away you go. Finally, the client comes back and says that he really wants the data to be formatted and sent as an emailand then, what the heck, why not create XML-formatted files as well?

Although you might want to pull your hair out in frustration, you’re really not in a bad situation. If you separate the compilation and storage classes from the formatting classesone for each of the various delivery methods (HTML, text, email, XML)you essentially have a parent-child relationship. Consider the parent class the one that holds the compilation and storage methods. The formatting classes are the childrenthey inherit the information from the parent and output the result based on their own functionality. Everybody wins.