Low cost ecommerce web development India flash website design

 

class PoliteMessage extends Message {

function PoliteMessage()

{

$this->setMessage('How are you today?');

}

}

class TerseMessage extends Message {

function TerseMessage()

{

$this->setMessage('Howzit?');

}

}

class RudeMessage extends Message {

function RudeMessage()

{

$this->setMessage('You look like *%&* today!');

}

}

Now, we define the MessageReader class, which takes an array of Message objects

through its constructor.

File: 14.php (excerpt)

class MessageReader {

var $messages;

function MessageReader(&$messages) {

$this->messages = &$messages;

$this->readMessages();

}

function readMessages() {

foreach ($this->messages as $message) {

echo $message->getMessage() . '<br />';

}

}

}

The important thing to note here is that, as far as MessageReader is concerned,

a “Message object” is any object that was instantiated from the Message class or

one of its subclasses. Did you see how, inside the readMessages method, we call

the getMessage method? This code will work on any object that has a getMessage

method—including any subclass of Message.

Polymorphism

Now, to prove the point, let's create some Message objects using our three subclasses

at random:

File: 14.php (excerpt)

$classNames =

array('PoliteMessage', 'TerseMessage', 'RudeMessage');

$messages = array();

srand((float)microtime() * 1000000); // Prepares random shuffle

for ($i = 0; $i < 10; $i++) {

shuffle($classNames);

$messages[] = new $classNames[0]();

}

$messageReader = new MessageReader($messages);

?>

By creating the array $classNames and then repeatedly shuffling it, we can take

the first element of the array and use it to create a new object:

$messages[] = new $classNames[0]();

This is an example of a variable function. The expression $classNames[0] is

evaluated to determine the name of the constructor (PoliteMessage,

TerseMessage, or RudeMessage) to call.

Finally, the $messages array contains ten messages, randomly selected, and is

passed to the constructor of MessageReader on instantiation.

Here's a sample result:

You look like *%&* today!

Howzit?

How are you today?

How are you today?

How are you today?

You look like *%&* today!

How are you today?

How are you today?

Howzit?

How are you today?

Each time we execute the script, the list is different.

Because all the concrete message classes share the same getMethod function (i.e.

they implement the same interface), the MessageReader class is able to extract

the data without knowing which particular type of message it's dealing with

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