假设我有这个经典的开关,我知道当我们构建类时,使用开关方法并不是一个好的实践,所以,我如何可以不使用 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提前谢谢你,希望这能帮到别人。
亲切的问候
卢卡
发布于 2011-02-17 18:01:10
我不完全确定您的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);发布于 2011-02-17 17:57:54
如果您真的想为此使用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' );发布于 2011-02-17 17:55:19
这里不需要面向对象。但是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;但是,由于苹果没有做任何事情,所以您可能不想为它们创建一个类或实际对象。(该死的苹果只是坐在那里什么也不做。
https://stackoverflow.com/questions/5032563
复制相似问题