DEV Community

Discussion on: Juniors Literally Can't Write Switch Statements: What Senior PHP Developers Need to Focus On

Collapse
 
vasilvestre profile image
Valentin Silvestre

But in fact, I'm trying to don't use switch function.. In most of case, if/else are cleaner.

Instead of this :

<?php

switch (true) {
    case $this->object instanceof SomeClass:
        //Do Something
    break;
    //etc, etc...

I prefer ternary operator or simple if :

<?php

if ($this->object instanceOf SomeClass) {
   // Do something
}

return $this->object instanceOf SomeClass ? 'foo' : 'bar';

And I'm triggered that there's no default in your switch !