| <home / programming / phpanth2 / 1 | [previous] [next] |
|
|
<?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:
$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:
<?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:
<?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?
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.
The following example implements a simple navigation system for a Web page, generating the HTML that appears at the top of the page. By having one class inherit from another, it becomes possible to add “crumb trail” navigation to the page when it’s needed.
First up, the StandardHeader class deals with generating the HTML for the top of the page, as well as supplying the setHeader and getHeader methods to access the variable where the HTML is stored.
<?php
/**
* A standard header for a Web page
*/
class StandardHeader {
/**
* The header HTML is stored here
*/
var $header = '';
/**
* The constructor, taking the name of the page
*/
function StandardHeader($title)
{
$html = <<<EOD
<html>
<head>
<title> $title </title>
</head>
<body>
<h1>$title</h1>
EOD;
$this->setHeader($html);
}
/**
* General method for adding to the header
*/
function setHeader($string)
{
if (!empty($this->header)) {
$this->header .= $string;
} else {
$this->header = $string;
}
}
/**
* Fetch the header
*/
function getHeader()
{
return $this->header;
}
}
Now, the subclass CategoryHeader brings extra functionality to its parent, adding the “bread crumb” links to the HTML that was generated. We don’t need to recreate the setHeader and getHeader methods, as these are inherited from StandardHeader when CategoryHeader is instantiated.
/**
* Subclass for dealing with Categories, building a breadcrumb
* menu
*/
class CategoryHeader extends StandardHeader {
/**
* Constructor, taking the category name and the pages base URL
*/
function CategoryHeader($category, $baseUrl)
{
// Call the parent constructor
parent::StandardHeader($category);
// Build the breadcrumbs
$html = <<<EOD
<p><a href="$baseUrl">Home</a> >
<a href="$baseUrl?category=$category">$category</a></p>
EOD;
// Call the parent setHeader() method
$this->setHeader($html);
}
}
Let’s now put these two classes to use:
// Set the base URL
$baseUrl = '12.php';
// An array of valid categories
$categories = array('PHP', 'MySQL', 'CSS');
// Check to see if we're viewing a valid category
if (isset($_GET['category']) &&
in_array($_GET['category'], $categories)) {
// Instantiate the subclass
$header = new CategoryHeader($_GET['category'], $baseUrl);
} else {
// Otherwise it's the home page. Instantiate the Parent class
$header = new StandardHeader('Home');
}
// Display the header
echo $header->getHeader();
?>
<h2>Categories</h2>
<p><a href="<?php echo $baseUrl; ?>?category=PHP">PHP</a></p>
<p><a href="<?php echo $baseUrl; ?>?category=MySQL">MySQL</a></p>
<p><a href="<?php echo $baseUrl; ?>?category=CSS">CSS</a></p>
</body>
</html>
As you can see, the controlling logic above looks for a $_GET['category'] variable. If it exists, it creates an instance of CategoryHeader, displaying the navigation to allow users to find their way back to the home page. But if it doesn’t exist, it creates an instance of the parent StandardHeader instead, which applies when users view the home page (and therefore does not require bread crumbs to find their way back).
In other words, inheritance allows us to add the extra functionality we need without having to reproduce the logic that already resides within the parent class; the existing methods and logic can be reused via the child subclass.
Inheritance provides a powerful mechanism to make classes that are modular, addressing a specific problem, while still making available shared methods and variables that can be used irrespective of the specific object we’re dealing with.
As a general rule of thumb, when using inheritance to build class hierarchies, avoid going deeper than two generations.
Doing so is often a sign of a bad design, in which opportunities for classes to interact in different ways (see the next solution) were missed. In practice, having more than two generations of classes often leads to all sorts of debugging problems and makes the code difficult to maintain. For example, it can become hard to keep track of variable names you’ve used higher up in the hierarchy.
| home / programming / phpanth2 / 1 | [previous] [next] |
Created: March 27, 2003
Revised: January 2, 2004
URL: http://webreference.com/programming/phpanth2