Article

The PHP Anthology Volume 1, Chapter 2 - Object Oriented PHP

Page: 1 2 3 4 5 6 7 8 9 10 11 Next

How do I take advantage of inheritance?

Inheritance is one of the fundamental pieces of the object oriented paradigm and is an important part of its power. Inheritance is a relationship between different classes in which one class is defined as being a child or subclass of another. The child inherits the methods and member variables defined in the parent class, allowing it to “add value” to the parent.

The easiest way to see how inheritance works in PHP is by example. Let’s say we have this simple class:

Example 2.13. 8.php (excerpt)

<?php        
class Hello {        
 function sayHello()        
 {        
   return 'Hello World!';        
 }        
}

Using the extends keyword, we can make a class that’s a child of Hello:

Example 2.14. 8.php (excerpt)

class Goodbye extends Hello {        
 function sayGoodbye()        
 {        
   return 'Goodbye World!';        
 }        
}

Goodbye is now a child of Hello. Expressed the other way around, Hello is the parent or superclass of Goodbye. Now, we can simply instantiate the child class and have access to the sayHello and the sayGoodbye methods using a single object:

Example 2.15. 8.php (excerpt)

$msg = &new Goodbye();        
       
echo $msg->sayHello() . '<br />';        
echo $msg->sayGoodbye() . '<br />';        
?>

That example shows the basics of how inheritance works, but doesn’t demonstrate its real power… This comes with the addition of overriding.

Overriding

What happens when we give a function in the child the same name as a function in the parent? An example:

Example 2.16. 9.php (excerpt)

<?php        
class Hello {        
 function getMessage()        
 {        
   return 'Hello World!';        
 }        
}        
       
class Goodbye extends Hello {        
 function getMessage()        
 {        
   return 'Goodbye World!';        
 }        
}

Both classes have the same method name, getMethod. This is perfectly acceptable to PHP—it makes no complaints about a method being declared twice.

Here’s what happens when we use the classes:

Example 2.17. 9.php (excerpt)

$hello = &new Hello();        
echo $hello->getMessage() . '<br />';        
       
$goodbye = &new Goodbye();        
echo $goodbye->getMessage() . '<br />';        
?>

And the output is as follows:

Hello World!        
Goodbye World!

Calling getMessage via the $goodbye object displays “Goodbye World!” The method in the child class is overrides the method in the parent class.

You can also have the child class make use of the parent class’s method internally, while overriding it. For example:

Example 2.18. 10.php

<?php        
class Hello {        
 function getMessage()        
 {        
   return 'Hello World!';        
 }        
}        
       
class Goodbye extends Hello {        
 function getMessage()        
 {        
   $parentMsg = parent::getMessage();        
   return $parentMsg . '<br />Goodbye World!';        
 }        
}        
       
$goodbye = &new Goodbye();        
echo $goodbye->getMessage() .'<br />';        
?>

Using the parent keyword, we can call the parent class’s method.

Note that we can also call the parent class by name to achieve exactly the same result:

class Goodbye extends Hello {        
 function getMessage() {        
   $parentMsg = Hello::getMessage();        
   return $parentMsg . '<br />Goodbye World!';        
 }        
}

Notice that we’ve replaced the parent keyword with the name of the Hello class. The output is exactly the same. Using parent, however, saves you from having to remember the name of the parent class while working in the child, and is the recommended syntax.

A call such as parent::getMessage() or Hello::getMessage() from a non-static method is not the same as calling a static function. This is a special case where inheritance is concerned. The called function in the parent class retains access to the instance data, and is therefore not static. This may be demonstrated as follows:

Example 2.19. 11.php

<?php        
class A {        
   var $a = 1;        
   function printA()        
   {        
       echo $this->a;        
   }        
}        
       
class B extends A {        
   var $a = 2;        
   function printA()        
   {        
      parent::printA();        
      echo "\nWasn't that great?";        
   }        
}        
       
$b = new B();        
$b->printA();        
?>

The output generated from the above is as follows:

2

Wasn't that great?

PHP does not cascade constructors

Most object oriented languages, like Java, will run the constructor of the parent class automatically, before running an overriding constructor in the child class. This is called cascading constructors—it’s a feature that PHP does not have.

If you create a constructor in a child class, be aware that you are completely overriding the parent class’s constructor, and that you must call it explicitly from your new constructor if you still want the parent class to handle its share of the object initialization.

Overriding declared member variables is achieved in exactly the same way as methods, although you’re unlikely to use this feature frequently.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links