note 100615 added to language.oop5.abstract

From: Date: Wed, 27 Oct 2010 02:58:23 +0000
Subject: note 100615 added to language.oop5.abstract
Groups: php.notes 
Request: Send a blank email to php-notes+get-173487@lists.php.net to get a copy of this message
While working on some wrapper functions, I was using code similar to this: <?php abstract class Dog { abstract public function bark(); } class Chihuahua extends Dog { public function bark($yip) { if($yip == true) { echo "YIP YIP YIP YIP"; } } } $c = new Chihuahua; $c->bark(true); ?> But kept on getting the error: Fatal error: Declaration of Chihuahua::bark() must be compatible with that of Dog::bark() in C:\xampplite\htdocs\base\index.php on line 23 due to Dog::bark NOT having $yip, but Chihuahua::bark having the extra parameter. My solution was to make all of my function parameters, optional. For example: <?php abstract class Dog { abstract public function bark(); } class Chihuahua extends Dog { public function bark($yip = false) { if($yip == true) { echo "YIP YIP YIP YIP"; } else { echo "WOOF"; } } } ?> By making the "yip" parameter on Chihuahua::bark default to "false", I can have additional parameters in my function declaration without errors. ---- Server IP: 69.147.83.197 Probable Submitter: 203.206.183.102 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.abstract.php Edit -- https://master.php.net/note/edit/100615 Del: integrated -- https://master.php.net/note/delete/100615/integrated Del: useless -- https://master.php.net/note/delete/100615/useless Del: bad code -- https://master.php.net/note/delete/100615/bad+code Del: spam -- https://master.php.net/note/delete/100615/spam Del: non-english -- https://master.php.net/note/delete/100615/non-english Del: in docs -- https://master.php.net/note/delete/100615/in+docs Del: other reasons-- https://master.php.net/note/delete/100615 Reject -- https://master.php.net/note/reject/100615 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#173487) next »