Low cost ecommerce web development India flash website design

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:

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

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

File: 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.

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