简体   繁体   中英

PHP5 - OOP - Polymorphism - Help me to rewrite this simple switch

Assuming i have this classic switch , i know that when we are building classes is not a good practice use the switch method, so, how i can rebuild this into a class without using switch but Polymorphism and i would like to understand the approach.

/**
 * globals below are 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 i would like to be able to use it something like this

$AppleProps = new getApple('Granny_Smith'); // $AppleProps->Color, etc etc

Thank you in advance and hope this can help someone else.

Kind Regards

Luca

If you really want to use OO for this, then what you should do is create an appleFactory class, then have separate classes for each kind of apple...

class appleFactory
{
    public static function getApple( $name )
    {
        $className = $name.'_apple';

        return new $className( );
    }
}

class fuji_apple
{
    public function __construct( )
    {
        $this->color = 'Yellowish green';
        $this->size = 'medium';
        $this->origin = 'Japan';
        $this->season = 'October - January';
        $this->appleId = $Fuji['Fuji_id']; 
    }
}

class gala_apple
{
    public function __construct( )
    {
        $this->color = 'Yellow';
        $this->size = 'medium';
        $this->origin = 'New Zealand';
        $this->season = 'October - January';
        $this->appleId = $Gala['Gala_id']; 
    }
}

Then use it like so...

$fuji = appleFactory::get( 'fuji' );
$gala = appleFactory::get( 'gala' );

I'm not complete sure what your IDs mean, but this code gives you an AppleFactory that will "stamp" each new apple with a 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);

There's no need for object-orientation here. But the switch can be replaced by a much simpler construct. If you use a data array, you can even skip the function:

$apple_data = array(
  'Fuji' => array(
    'Color' => 'Yellowish green';
    'Size' => 'medium';
    'Origin' => 'Japan';
    'Season' => 'October - January';
    'AppleId' = 1234567890,
  ),
  'Gala' => array(
    'Color' => 'yellow';
    'Size' => 'medium';
    'Origin' => 'New Zealand';
    'Season' => 'October - January';
    'AppleId' => 1234598760,
  ),
  ...
);

To access the attributes just use:

$id = $apple_data["Granny_Smith"]["AppleId"]

Or if you really want all those local variables:

extract($apple_data["Granny_Smith"]);
// creates $Color, $Size, $Origin, $Season, $AppleId in local scope

If you really want the object-syntax, then try:

$AppleProps = new ArrayObject($apple_data["Fuji"], 2);
print $AppleProps->Color;

But since the apples are not doing anything, you probably don't want to create a class or real objects for them. (Damn apples. Just sitting there and doing nothing.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM