Prev | Current Page 41 | Next

Hasin Hayder

"Object-Oriented Programming with PHP5"

aclass.php on
line 8

Polymorphism
As we explained before, polymorphism is the process of creating several objects from
specific base classes. For example, take a look at the following case in point. We need
the three classes that we created earlier in this chapter, Emailer, ExtendedEmailer
and HtmlEmailer. Let's take a look at the following code.
include("class.emailer.php");
include("class.extendedemailer.php");
include("class.htmlemailer.php");
$emailer = new Emailer("hasin@somewherein.net");
$extendedemailer = new ExtendedEmailer();
$htmlemailer = new HtmlEmailer("hasin@somewherein.net");
if ($extendedemailer instanceof emailer )
echo "Extended Emailer is Derived from Emailer.
";
if ($htmlemailer instanceof emailer )
echo "HTML Emailer is also Derived from Emailer.
";
if ($emailer instanceof htmlEmailer )
echo "Emailer is Derived from HTMLEmailer.
";
if ($htmlemailer instanceof extendedEmailer )
echo "HTML Emailer is Derived from Emailer.
";
?>
If you execute the script above, you will find the following output:
Extended Emailer is Derived from Emailer.
HTML Emailer is also Derived from Emailer.
Kick-Starting OOP
[ 32 ]
This is an example of polymorphism.


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