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. An example is shown in Listing 7.8.

Listing 7.8. 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 listing7.8.php, place it in your document root, and access ...

Get Sams Teach Yourself PHP, MySQL® and Apache All in One now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.