note 66139 added to language.oop5.basic
| From: | Neunerball at hotmail dot com | Date: | Mon, 15 May 2006 20:43:01 +0000 |
| Subject: | note 66139 added to language.oop5.basic | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-111339@lists.php.net to get a copy of this message | ||
Here's the proper way:
<?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 class is showing HOW IT IS DONE
*************************************/
class ColoredGuineaPig2 extends GuineaPig
{
public $color;
/* constructor */
public function __construct($length = 10, $color = "brown") {
/*
* Call the parent's constructor. So we
* have the DOB initialized as well
*/
parent::__construct($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;
}
/**************************************
* instead of overwriting the method,
* use the parent method and append
* the new message
**************************************/
public function whoAreYou() {
$message = parent::whoAreYou();
$message .= "My fur is ".$this->getColor()."<br /><br
/>\r\n";
return $message;
}
} // end of class ColoredGuineaPig2
$gp2_A = new ColoredGuineaPig2(15,'white');
$gp2_B = new ColoredGuineaPig2;
// The dentist learned his job
$gp2_A->setLength(9);
/* let's print again */
print $gp2_A->whoAreYou();
print $gp2_A->getDateOfBirth()."<br />\r\n";
print $gp2_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/66139
Del: integrated -- http://master.php.net/note/delete/66139/integrated
Del: useless -- http://master.php.net/note/delete/66139/useless
Del: bad code -- http://master.php.net/note/delete/66139/bad+code
Del: spam -- http://master.php.net/note/delete/66139/spam
Del: non-english -- http://master.php.net/note/delete/66139/non-english
Del: in docs -- http://master.php.net/note/delete/66139/in+docs
Del: other reasons-- http://master.php.net/note/delete/66139
Reject -- http://master.php.net/note/reject/66139
Search -- http://master.php.net/manage/user-notes.php