note 66136 added to language.oop5.basic
| From: | Neunerball at hotmail dot com | Date: | Mon, 15 May 2006 20:39:49 +0000 |
| Subject: | note 66136 added to language.oop5.basic | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-111338@lists.php.net to get a copy of this message | ||
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();
?>
----
Server IP: 216.194.113.175
Probable Submitter: 69.15.234.167
----
X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HTML_MESSAGE autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/language.oop5.basic.php
Edit -- http://master.php.net/note/edit/66136
Del: integrated -- http://master.php.net/note/delete/66136/integrated
Del: useless -- http://master.php.net/note/delete/66136/useless
Del: bad code -- http://master.php.net/note/delete/66136/bad+code
Del: spam -- http://master.php.net/note/delete/66136/spam
Del: non-english -- http://master.php.net/note/delete/66136/non-english
Del: in docs -- http://master.php.net/note/delete/66136/in+docs
Del: other reasons-- http://master.php.net/note/delete/66136
Reject -- http://master.php.net/note/reject/66136
Search -- http://master.php.net/manage/user-notes.php