note 66136 deleted from language.oop5.basic by bjori

From: Date: Mon, 15 May 2006 20:51:58 +0000
Subject: note 66136 deleted from language.oop5.basic by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-111340@lists.php.net to get a copy of this message
Note Submitter: Neunerball at hotmail dot com Reason: useless ---- Some code from a german tutorial at URL:http://tut.php-q.net/klassen.html showing a bad example subclass (the good to follow) <?php class GuineaPig { /* declare some members */ public $hungry = 0; public $length = 100; // Note: default values are not mandatory // they can be declared in the constuctor public $dateOfBirth = "not born yet"; /* constructor */ public function __construct($length = 10) { $this->setLength($length); $this->setDateOfBirth(); } /* methods */ public function getLength() { return $this->length/10; // to make the mistake more visible } public function setLength($newLength) { if( !is_numeric($newLength) ){ return false; } $this->length = (int)($newLength * 10); return $this->length; } public function whoAreYou() { $message = "I'm a Guinea Pig and my teeth are ".$this->getLength()." milli meter long.<br />\r\n I was born on ".$this->getDateOfBirth()."<br />\r\n"; return $message; } public function getDateOfBirth() { return $this->dateOfBirth; } public function setDateOfBirth() { $this->dateOfBirth = date('l dS \of F Y'); return $this->dateOfBirth; } } // end of class GuineaPig /****************************************** * Now let's check out "inheritance". * We are planning to create a subclass of * GunieaPig that prints a color as well ****************************************** * This subclass is showing how it is NOT done * (See output) ******************************************/ class ColoredGuineaPig1 extends GuineaPig { public $color; /* constructor */ public function ColoredGuineaPig1($length = 10, $color = "brown") { $this->setLength($length); $this->setColor($color); } /* methods */ public function getColor() { return $this->color; } public function setColor($newColor) { switch($newColor) { case is_numeric: return false; break; case !is_string: return false; break; } $this->color = $newColor; return $this->color; } /************************************** * overwriting the method whoAreYou() * This is a big mistake !!! **************************************/ public function whoAreYou() { $message = "I'm a Guinea Pig and my teeth are ".$this->getLength()." milli meter long.<br />\r\n My fur is ".$this->getColor()."<br />\r\n"; return $message; } } // end of class ColoredGuineaPig1 $gp1_A = new ColoredGuineaPig1(15,'white'); $gp1_B = new ColoredGuineaPig1; // The dentist of gp1_A thinks it's teeth are to long. // So, he shortens them to 9 milli meter. /* BIG NO NO: */ $gp1_A->length = 9; print $gp1_A->whoAreYou(); print "<b><l>Damn, it should be 9 milli meter not 0.9!!! -> The dentist didn't use setLength().</l></b><br />\r\n"; print $gp1_A->getDateOfBirth(); print "<br /><b><l>What's happening? -> The constructor of ColoredGuineaPig1 doesn't initialize date of birth</l></b><br />\r\n"; print $gp1_B->whoAreYou(); ?>

« previous php.notes (#111340) next »