spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

<home / programming / phpanth2 / 1 To page 1To page 2To page 3To page 4To page 5current pageTo page 7
[previous] [next]

Data Center Architect
The Computer Merchant, Ltd
US-MA-chelsea

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


PHP Anthology, Volume 1, Chapter 2. Object Oriented PHP

Overriding

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

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:

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:

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:

The output generated from the above is as follows:

2
Wasn't that great?

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

Inheritance in Action

Now that you have a rough idea of how inheritance is used in PHP, it’s time to look at an example that should give you a better idea of how inheritance can be applied.

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.

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.

Let’s now put these two classes to use:

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.


home / programming / phpanth2 / 1 To page 1To page 2To page 3To page 4To page 5current pageTo page 7
[previous] [next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

Created: March 27, 2003
Revised: January 2, 2004

URL: http://webreference.com/programming/phpanth2