Low cost ecommerce web development India flash website design

What happens when we give a function in the child the same name as a function

in the parent? An example:

Overriding

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

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

File: 10.php

<?php

class Hello {

function getMessage()

{

Chapter 2: Object Oriented PHP

Buy Volume I and II — SAVE 25%! 50

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:

File: 11.php

<?php

class A {

var $a = 1;

function printA()

{

Overriding

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?

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