Construction of plant-based facilities



  • Ask the factory designer to transfer values to generating facilities or to create a new install, Chet was completely confused.

    class carFactory {
    
    public function __construct() {
    }
    
    public static function build ($type = '') {
    
        if($type == '') {
            throw new Exception('Invalid Car Type.');
        } else {
            $className = 'car_'.ucfirst($type);
            if(class_exists($className)) {
                return new $className();
            } else {
                throw new Exception('Car type not found.');
            }
        }
    }
    

    }

    class car_Sedan {

    public function __construct() {
        echo "Creating Sedan";
    }
    

    }
    class car_Suv {
    public function __construct() {
    echo "Creating SUV";
    }
    }



  • <?php
    class carFactory {
    
        public function __construct() {
        }
    
        public static function build ($type = '', $options = []) {
    
            if($type == '') {
                throw new Exception('Invalid Car Type.');
            } else {
                $className = 'car_'.ucfirst($type);
                if(class_exists($className)) {
                    return new $className($options);
                } else {
                    throw new Exception('Car type not found.');
                }
            }
        }
    }
    
    class Car
    {
        public function __construct($options = [])
        {
            var_dump($options);
        }
    }
    
    class car_Sedan extends Car {
    
        public function __construct($options) {
            parent::__construct($options);
            echo "Creating Sedan";
        }
    }
    class car_Suv extends Car {
        public function __construct($options) {
            parent::__construct($options);
            echo "Creating SUV";
        }
    }
    
    carFactory::build('Suv', ['wheels' => 5]);
    echo PHP_EOL;
    carFactory::build('Sedan', ['wheels' => 4]);
    
    /**
    array(1) {
      ["wheels"]=>
      int(5)
    }
    Creating SUV
    array(1) {
      ["wheels"]=>
      int(4)
    }
    Creating Sedan
    **/
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2