Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3991812
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:39:38+00:00 2026-05-20T06:39:38+00:00

Assuming i have this classic switch , i know that when we are building

  • 0

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

  • thank’s to http://www.allaboutapples.com/ for Apples Informations 😉
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T06:39:39+00:00Added an answer on May 20, 2026 at 6:39 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this repeated code below and I am assuming that this can be
I can't really understand this concept. Assuming i have this interface: public interface IValidate
Assuming that I have this navigation bar <div id = nav> <a class =
Assuming that I have objects similar to this: public class User { public int
Assuming I have this HTML structure: <ul class='menu'> <li> <div></div> <div> <div></div> <div> <a
Assuming I have a function like this my_method(const vector<const T*> & param); I wonder
Assuming I have a JavaScript function like this... function Object1() { this.var1; this.var2; this.var3;
Assuming I have a comments model and a posts model, What code can I
Assuming I have to use C (no C++ or object oriented compilers) and I
Assuming we have a salt that's in the database and that has been generated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.