PHP5 - OOP - 多态 - 帮我重写这个简单的开关

时间:2011-02-17 17:46:39

标签: php oop polymorphism switch-statement

假设我有这个经典的开关,我知道当我们构建类时使用switch方法不是一个好习惯,所以,如何在不使用的情况下将其重建为类切换多态,我想了解这种方法。

/**
 * 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;
}

然后我希望能够使用类似这样的东西

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

提前感谢您,希望这可以帮助其他人。

亲切的问候

卢卡

  

3 个答案:

答案 0 :(得分:5)

如果你真的想为此使用OO,那么你应该做的是创建一个appleFactory类,然后为每种苹果都有单独的类......

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']; 
    }
}

然后像这样使用它......

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

答案 1 :(得分:4)

我不完全确定您的ID是什么意思,但是这段代码为您提供了一个AppleFactory,它将使用唯一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);

答案 2 :(得分:3)

这里不需要面向对象。但switch可以用更简单的结构代替。如果使用数据数组,甚至可以跳过该功能:

$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,
  ),
  ...
);

要访问属性,请使用:

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

或者如果你真的想要所有这些局部变量:

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

如果你真的想要对象语法,那么试试:

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

但是由于苹果没有做任何事情,你可能不想为它们创建一个类或真实对象。 (该死的苹果。只是坐在那里什么都不做。)