note 66139 deleted from language.oop5.basic by bjori

From: Date: Mon, 15 May 2006 20:52:09 +0000
Subject: note 66139 deleted from language.oop5.basic by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-111341@lists.php.net to get a copy of this message
Note Submitter: Neunerball at hotmail dot com Reason: useless ---- 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(); ?>

« previous php.notes (#111341) next »