note 79349 deleted from language.oop5 by felipe
| From: | felipe@php.net | Date: | Mon, 24 Mar 2008 22:10:29 +0000 |
| Subject: | note 79349 deleted from language.oop5 by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-137467@lists.php.net to get a copy of this message | ||
Note Submitter: SID TRIVEDI
----
/**
* TITLE : DIFFERENCE BETWEEN PHP OBJECT ORIENTED PROGRAMMING & PHP PROCEDURAL PROGRAMMING.
* SCOPE : To explain / understand PHP OOPs in an easier way.
* Credit : Practical PHP Programming by PAUL HUDSON (http://www.hudzilla.org/phpbook/index.php)
*/
class dog {
function bark() {
print "Grrrrrrr...Woof...Woof!\n<br>";
}
}
class poodle extends dog {
function bark() {
print "Yip...Yeep...Yeeeeeeeeeeeeep!\n<br>";
}
}
$poppy = new dog;
$poppy->Name = "Poppy";
print $poppy->Name;
echo "\n<br>";
$poppy->bark(); // function call bark() with class as dog
$penny = new poodle;
$penny->Name = "Penny";
print $penny->Name;
echo "\n<br>";
$penny->bark(); // function call bark() with extended class as poodle
/*
$german_sheppard = new dog;
$german_sheppard->Name = "German Sheppard";
print $german_sheppard->Name;
echo "\n<br>";
$german_sheppard->bark(); // function call bark() with class as dog
*/
/* Above OOP - Object Oriented Programming approach (codes) produces following (same) browser
output:
Poppy
Grrrrrrr...Woof...Woof!
Penny
Yip...Yeep...Yeeeeeeeeeeeeep!
The difference between procedural programming and an OOP aproach is ease of manipluating around the
objects and its class. Lets say we wanna add German Sheppard to dog class, its easy....
*/
// Now producing same browser output with Procedural Programming Apprach:
/*
$dog = null; // Initial definition and value assignment of variable $dog
$poppy = 'Poppy';
$penny = 'Penny';
//$german_sheppard = 'German Sheppard';
function bark($dog){
if ($dog === 'Poppy'){
print "$dog\n<br>";
print "Grrrrrrr...Woof...Woof!\n<br>";
return;
}
if($dog === 'Penny'){
print "$dog\n<br>";
print "Yip...Yeep...Yeeeeeeeeeeeeep!\n<br>";
return;
}
// if ($dog === 'German Sheppard'){
// print "$dog\n<br>";
// print "Grrrrrrr...Woof...Woof!\n<br>";
// return;
// }
else{
return;
}
}
bark ($poppy); //calling function bark on variable $poppy-->$dog
bark ($penny); //calling function bark on variable $penny-->$dog
//bark ($german_sheppard); // calling function bark on cariable $german_sheppard-->$dog
*/
/* Above Proceduaral codes produces following browser output:
Poppy
Grrrrrrr...Woof...Woof!
Penny
Yip...Yeep...Yeeeeeeeeeeeeep!
Same output result can be produced by OOP approach as shown above.
*/
?>