How can i optimize and reduce the inheritance child class php code

class Maruti{
 public $currentSpeed;
 
 public function __construct($speed){
    $this->currentSpeed = $speed;
 }
 public function increaseSpeed($boost){
    if($boost == 'bad'){
        $this->currentSpeed -=5;
    }elseif($boost == 'fair'){
         $this->currentSpeed +=5;
    }elseif($boost == 'good'){
         $this->currentSpeed +=10;
    }elseif($boost == 'turbo'){
        $this->currentSpeed +=20;
    }
    if($this->currentSpeed == 100 || $this->currentSpeed == 125 || $this->currentSpeed == 150){
        echo "Congratulations! You have reached a speed of $this->currentSpeed";
    }
 }    

}

class BMW extends Maruti{
public function increaseSpeed($boost){
   if($boost == 'fair'){
         $this->currentSpeed +=10;
    }elseif($boost == 'good'){
         $this->currentSpeed +=15;
    }elseif($boost == 'turbo'){
        $this->currentSpeed +=25;
    }
    if($this->currentSpeed == 100 || $this->currentSpeed == 125 || $this->currentSpeed == 150){
        echo "Congratulations! You have reached a speed of $this->currentSpeed";
    }
 }    

}

 $obj = new BMW(115);
 $obj->increaseSpeed('fair');

How Can i reduce the code inside BMW class,How can i use additional variable to reduce the child class code, Please provide me the complete code with exact output.



Read more here: https://stackoverflow.com/questions/68464340/how-can-i-optimize-and-reduce-the-inheritance-child-class-php-code

Content Attribution

This content was originally published by user3505085 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: