3. Let's Bake Some Objects <? //emailer.class..php class Emailer { private $sender; private $recipients; private $subject; private $body; function __construct($sender) { $this->sender = $sender; $this->recipients = array(); } public function addRecipients($recipient) { array_push($this->recipients, $recipient); } public function setSubject($subject) { $this->subject = $subject; } public function setBody($body) { $this->body = $body; } public function sendEmail() { foreach ($this->recipients as $recipient) { $result = mail($recipient, $this->subject, $this->body, "From: {$this->sender}"); if ($result) { echo "Mail successfully sent to {$recipient}<br/>"; } } } } ?>
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14. Constructors and Destructors #2 <? //factorial.class.php class factorial { private $result = 1;// you can initialize directly outside private $number; function __construct($number) { $this->number = $number; for($i=2; $i<=$number; $i++) { $this->result *= $i; } } public function showResult() { echo "Factorial of {$this->number} is {$this->result}. "; } } ?>
15.
16.
17. Class Constants#2 <? include_once("class.wordcounter.php"); $wc = new WordCounter("words.txt"); $wc->count(WordCounter::DESC); ?>
18.
19. Extending a Class [Inheritance] #2 <? class HtmlEmailer extends emailer { public function sendHTMLEmail() { foreach ($this->recipients as $recipient) { $headers = 'MIME-Version: 1.0' . ""; $headers .= 'Content-type: text/html; charset=iso-8859-1' . ""; $headers .= 'From: {$this->sender}' . ""; $result = mail($recipient, $this->subject, $this->body, $headers); if ($result) echo "HTML Mail successfully sent to {$recipient}<br/>"; } } } ?>
20. Extending a Class [Inheritance] #3 <? include_once("class.htmlemailer.php"); $hm = new HtmlEmailer(); //.... do other things $hm->sendEmail(); $hm->sendHTMLEmail(); ?>
21.
22.
23.
24.
25. <? include("emailer.class.php"); include("extendedemailer.class.php"); include("htmlemailer.class.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.<br/>"; if ($htmlemailer instanceof emailer ) echo "HTML Emailer is also Derived from Emailer.<br/>"; if ($emailer instanceof htmlEmailer ) echo "Emailer is Derived from HTMLEmailer.<br/>"; if ($htmlemailer instanceof extendedEmailer ) echo "HTML Emailer is Derived from Emailer.<br/>"; ?> Extended Emailer is Derived from Emailer. HTML Emailer is also Derived from Emailer. The output
26.
27. Interface #2 <? //dbdriver.interface.php interface DBDriver { public function connect(); public function execute($sql); } ?> <? //mysqldriver.class.php include("dbdriver.interface.php"); class MySQLDriver implements DBDriver { } ?> implement Error !
28. Interface #2 <? //dbdriver.interface.php interface DBDriver { public function connect(); public function execute($sql); } ?> <? include("interface.dbdriver.php"); class MySQLDriver implements DBDriver { public function connect() { //connect to database } public function execute() { //execute the query and output result } } ?> implement Error ! The execute() does'nt have an argument
29. Interface #3 <? //dbdriver.interface.php interface DBDriver { public function connect(); public function execute($sql); } ?> <? include("interface.dbdriver.php"); class MySQLDriver implements DBDriver { public function connect() { //connect to database } public function execute() { //execute the query and output result } } ?> implement public function execute($query) { //execute the query and output result } Error ! The execute() does'nt have an argument Should have one argument
30.
31. Abstract Class #2 <?php //reportgenerator.abstract.php abstract class ReportGenerator { public function generateReport($resultArray) { //write code to process the multidimensional result array and //generate HTML Report } } ?>
32. Abstract Class #3 <?php include("dbdriver.interface.php"); include("reportgenerator.abstract.php"); class MySQLDriver extends ReportGenerator implements DBDriver { public function connect() { //connect to database } public function execute($query) { //execute the query and output result } // You need not declare or write the generateReport method here //again as it is extended from the abstract class directly." } ?>
33.
34.
35. Static Method and Properties #2 <?php //dbmanager.class.php class DBManager { public static function getMySQLDriver() { //instantiate a new MySQL Driver object and return } public static function getPostgreSQLDriver() { //instantiate a new PostgreSQL Driver object and return } public static function getSQLiteDriver() { //instantiate a new MySQL Driver object and return } } ?>
36.
37. How static property actually works ? <?php //statictester.class.php class StaticTester { private static $id=0; function __construct() { self::$id +=1; } public static function checkIdFromStaticMehod() { echo "Current Id From Static Method is ".self::$id.""; } public function checkIdFromNonStaticMethod() { echo "Current Id From Non Static Method is ".self::$id.""; } } $st1 = new StaticTester(); StaticTester::checkIdFromStaticMehod(); $st2 = new StaticTester(); $st1->checkIdFromNonStaticMethod(); //returns the val of $id as 2 $st1->checkIdFromStaticMehod(); $st2->checkIdFromNonStaticMethod(); $st3 = new StaticTester(); StaticTester::checkIdFromStaticMehod(); ?> Current Id From Static Method is 1 Current Id From Non Static Method is 2 Current Id From Static Method is 2 Current Id From Non Static Method is 2 Current Id From Static Method is 3 The Output : Using this special facility, a special design pattern "Singleton" works perfectly in PHP.
38.
39. Accesor Methods <?php class Student { private $name; private $roll; public function setName($name) { $this->name= $name; } public function setRoll($roll) { $this->roll =$roll; } public function getName() { return $this->name; } public function getRoll() { return $this->roll; } } ?> But what happen if we have lots of properties ? Do we have to create accesor method for all the properties ? No ..! We can use magic method to achieve this...
40.
41. Using Magic Method to Set/Get Class Properties #2 <?php //student.class.php class Student { private $properties = array(); function __get($property) { return $this->properties[$property]; } function __set($property, $value) { $this->properties[$property]="AutoSet {$property} as: ".$value; } } ?> <?php $st = new Student(); $st->name = "Afif"; $st->roll=16; echo $st->name.""; echo $st->roll; ?> AutoSet name as: Afif AutoSet roll as: 16
42.
43. Magic Methods for Overloading Class Methods <?php class Overloader { function __call($method, $arguments) { echo "You called a method named {$method} with the following arguments <br/>"; print_r($arguments); echo "<br/>"; } } $ol = new Overloader(); $ol->access(2,3,4); $ol->notAnyMethod("boo"); ?> You called a method named access with the following arguments Array ( [0] => 2 [1] => 3 [2] => 4 ) You called a method named notAnyMethod with the following arguments Array ( [0] => boo )