note 79349 added to language.oop5

From: Date: Thu, 22 Nov 2007 10:27:30 +0000
Subject: note 79349 added to language.oop5
Groups: php.notes 
Request: Send a blank email to php-notes+get-132479@lists.php.net to get a copy of this message
/** * 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. */ ?> ---- Server IP: 69.147.83.197 Probable Submitter: 208.72.122.17 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.php Edit -- https://master.php.net/note/edit/79349 Del: integrated -- https://master.php.net/note/delete/79349/integrated Del: useless -- https://master.php.net/note/delete/79349/useless Del: bad code -- https://master.php.net/note/delete/79349/bad+code Del: spam -- https://master.php.net/note/delete/79349/spam Del: non-english -- https://master.php.net/note/delete/79349/non-english Del: in docs -- https://master.php.net/note/delete/79349/in+docs Del: other reasons-- https://master.php.net/note/delete/79349 Reject -- https://master.php.net/note/reject/79349 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#132479) next »