I have to make a function to assign a string to a variable according to the value of other variable using OOP. I created this function (calculaIMC) inside a constructor class, and to achieve that I used an array ($imc_arr). I realize my code probably looks cumbersome and even inappropriate perhaps but it's an uni exercise designed to teach specific things. That's what I came up with so far:
<?php
class CalculoIndice{
...
public $imc_arr = array(
'Magreza grave',
'Magreza moderada',
'Magreza leve',
'Saudável',
'Sobrepeso',
'Obesidade Grau I',
'Obesidade Grau II (severa)',
'Obesidade Grau III (mórbida)');
function CalculoIndice(){
$this->preparaCalculo();
$this->calculaIMC();
}
function preparaCalculo(){
...
}
function calculaIMC(){
switch ($this->imc) {
case ($this->imc < 16):
$this->imc_cat = $this->imc_arr[0];
break;
case ($this->imc < 17):
$this->imc_cat = $this->imc_arr[1];
break;
case ($this->imc < 18.5):
$this->imc_cat = $this->imc_arr[2];
break;
case ($this->imc < 25):
$this->imc_cat = $this->imc_arr[3];
break;
case ($this->imc < 30):
$this->imc_cat = $this->imc_arr[4];
break;
case ($this->imc < 35):
$this->imc_cat = $this->imc_arr[5];
break;
case ($this->imc < 40):
$this->imc_cat = $this->imc_arr[6];
break;
default:
$this->imc_cat = $this->imc_arr[7];
}
}
}
?>
It is not working. I couldn't quite figure what is going on and what the problem is but it doesn't echo the variable (imc_cat) as I intended. I'm sure it's something simple that I am missing but I've spent a few hours looking for an answer with no success. I appreciate any insight as to what may be wrong.
$this->imc >0 && $this->imc < 16,$this->imc >16 && $this->imc < 18.5,$this->imc >18.5 && $this->imc < 25