Low cost ecommerce web development India flash website design

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.

File: 12.php (excerpt)

<?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;

}

}

Inheritance in Action

/**

* 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.

File: 12.php (excerpt)

/**

* 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:

File: 12.php (excerpt)

// 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>

website designer freelance ASP PHP ecommerce web developer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110