dimanche 28 juin 2020

Switch case of object in PHP

I don't have a good knowledge in PHP. Will you please, anyone help me to implement a switch case or if else statement in a set of objects.

For example I have set of JSON object like below

 {"car":[{"colour":"black", "brand":"BMW", "owner":"rob"}]}
 {"bike":[{"colour":"red",  "brand":"Bajaj", "owner":"john"}]}
 {"cycle":[{"colour":"blue",  "brand":"Hero", "owner":"mike"}]}

My requirement is need to check it's car, bike or cycle. in the below example message is the random JSON object

$smsobj = json_decode($message, true);
switch ($smsobj) {
    case $smsobj->bike:
      $this->bike($smsobj);
      break;
    case $smsobj->car:
      $this->car($smsobj);
      break;
    case $smsobj->cycle:
      $this->cycle($smsobj);
      break;
      defualt: $resolver->acknowledge($message);
  }

when I received a car object I will get some error like this Trying to get property 'bike' of non-object

Thank you.

Aucun commentaire:

Enregistrer un commentaire