php - PHP5 - OOP - Polymorphism - Help me to rewrite this simple switch -
assuming have classic switch, know when building classes not practice use switch method, so, how can rebuild class without using switch polymorphism , understand approach.
/** * globals below holding unique id * $franklin['franklin_id'] , * $granny_smith['granny_smith_id'] , * etc etc... */ global $fuji, $gala, $franklin, $granny_smith; switch($apple) { case 'fuji': $color = 'yellowish green'; $size = 'medium'; $origin = 'japan'; $season = 'october - january'; $appleid = $fuji['fuji_id']; break; case 'gala': $color = 'yellow'; $size = 'medium'; $origin = 'new zealand'; $season = 'october - january'; $appleid = $gala['gala_id']; break; case 'franklin': $color = 'well-colored'; $size = 'medium'; $origin = 'ohio'; $season = 'october'; $appleid = $franklin['franklin_id']; break; case 'granny_smith': $color = 'green'; $size = 'medium'; $origin = 'australia'; $season = 'october - december'; $appleid = $granny_smith['granny_smith_id']; break; }
then able use this
$appleprops = new getapple('granny_smith'); // $appleprops->color, etc etc
thank in advance , hope can else.
kind regards
luca
- thank's http://www.allaboutapples.com/ apples informations ;)
i'm not complete sure ids mean, code gives applefactory "stamp" each new apple unique id.
class applefactory { static $id = 0; static public function getapple($classname) { $apple = new $classname(); $apple->id = self::$id++; return $apple; } } class apple { public $id; public $color; public $size; public $origin; public $season; } class grannysmith extends apple { public function __construct() { $this->color = 'green'; $this->size = 'medium'; $this->origin = 'australia'; $this->season = 'october - desember'; } } $a = applefactory::getapple('grannysmith'); print_r($a);
Comments
Post a Comment