skip to Main Content

I would want to write the following code without switch, if else or the ternary operator. The code is shown below:

  switch (type){
  case 'bed':
     function create{
      return new Bed($customer, $price, $dimensions );
     }
      break;
  case 'desk':
     function create{
      return new Desk($customer, $price, $dimensions);
    }
      break;
  
  case 'chair':
       function create{
      return new Chair($customer, $price, $dimensions, $type );
   }
      break;

  }

4

Answers


  1. You can make the code more concise by returning a new instance of the class by using class name stored in $type. For calling the constructor with different parameters, make use of splat operator by collecting the params in an array.

    <?php
    
    function create($type){
        global $customer, $price, $dimensions;
        $className = ucfirst($type);
        $params = [$customer, $price, $dimensions, $type];
        return new $className(...$params);
    }
    
    create($type);
    
    Login or Signup to reply.
  2. If you want to call another class here, you can set the classes you want to global. Then in the functions below, you can call them from there using global.

    Example output:
    output photo

    <?php
      ini_set('display_errors', 1);
      ini_set('display_startup_errors', 1);
      error_reporting(E_ALL);
    
      class Bed{
        function __construct($customer, $price, $dimensions){
          echo "Ordered <b>Bed</b> for: <b>$customer</b> at the price of: <b>$price</b> and with the dimensions of: <b>$dimensions</b>";
        }
      }
      class Chair{
        function __construct($customer, $price, $dimensions){
          echo "Ordered <b>Chair</b> for: <b>$customer</b> at the price of: <b>$price</b> and with the dimensions of: <b>$dimensions</b>";
        }
      }
      class Desk{
        function __construct($customer, $price, $dimensions){
          echo "Ordered <b>Desk</b> for: <b>$customer</b> at the price of: <b>$price</b> and with the dimensions of: <b>$dimensions</b>";
        }
      }
    
      class Furniture{
    
        private function bed($customer, $price, $dimensions){
          new Bed($customer, $price , $dimensions);
        }
    
        private function chair($customer, $price, $dimensions){
          new Chair($customer, $price , $dimensions);
        }
    
        private function desk($customer, $price, $dimensions){
          new Desk($tcustomer, $price , $dimensions);
        }
    
        function __construct($type = null, $customer = null, $price = null, $dimensions = null) {
          return $this->$type($customer, $price, $dimensions);
        }
      }
    
      new Furniture('bed','Joes Momma','$1.99','4x5x2');
    
    Login or Signup to reply.
  3. you can use the same code in if else statement like below .
    if == do not work try to add === instead

    if(type == "bed"){
    
         function create{
          return new Bed($customer, $price, $dimensions );
         }}
    else if(type == "desk"){
    
         function create{
          return new Desk($customer, $price, $dimensions);
        }}
        else if(type == "chair"){
    
           function create{
          return new Chair($customer, $price, $dimensions, $type );
       }
      }
    
    Login or Signup to reply.
  4. 📎 – Looks like you’re trying to write a factory function.

    Instead of using a conditional, you can create a list of allowed types mapping to class creator functions.

    enum FurnitureType: string {
        case Bed = 'bed';
        case Desk = 'desk';
        case Chair = 'chair';
    }
    
    class FurnitureFactory {
        private $creators;
        
        public function __construct() {
            // ideally this would be static but PHP no-likey
            $this->creators = [
                FurnitureType::Bed->value => static fn($customer, $price, $dimensions) =>
                    new Bed($customer, $price, $dimensions),
                FurnitureType::Desk->value => static fn($customer, $price, $dimensions) =>
                    new Desk($customer, $price, $dimensions),
                FurnitureType::Chair->value => static fn($customer, $price, $dimensions) =>
                    new Chair($customer, $price, $dimensions, FurnitureType::Chair->value),
            ];
        }
    
        public function creator(FurnitureType $type, $customer, $price, $dimensions) {
            $creator = $this->creators[$type->value] ??
                throw new Exception("Not implemented for type {$type->name}");
            return fn() => $creator($customer, $price, $dimensions);
        }
    }
    
    $factory = new FurnitureFactory();
    $bedCreator = $factory->creator(FurnitureType::Bed, 'customer', 100, '200x100');
    print_r($bedCreator());
    
    $chairCreator = $factory->creator(FurnitureType::Chair, 'customer', '50', '20x50');
    print_r($chairCreator());
    

    Requires PHP 8.1.0 or later

    https://3v4l.org/fDuKG#v8.1.12

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search