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

Assuming I have this classic switch , I know that when we create classes, it is not recommended to use the switch method, therefore, how can I rebuild this into a class without using switch , but Polymorphism , and I would like to understand this 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 use it somehow like this

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

Thank you in advance and hope this helps someone else.

Yours faithfully

Luke

+3
source share
3 answers

, , AppleFactory, "" .

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);
+4

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

...

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

There is no need for object orientation. But switchit can be replaced by a much simpler design. If you are using 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 you just used:

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

Or, if you really want all these local variables:

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

If you really need the syntax of an object, try:

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

But since apples do nothing, you probably don't want to create a class or real objects for them. (Damn apples, just sitting and doing nothing.)

+3
source

Source: https://habr.com/ru/post/1792867/


All Articles