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

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

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -