Switch Hell in PHP

Warning: Rabbit hole below from a wandering mind
My ideas below will not cover every niche or hidden function of PHP’s switch nor do I make any claims that these can be considered ‘good practice’.

Typical simple switch statement

Not much to say… Simple with a few; Crazy with a lot.

$switchedvar = 'foo';
switch($switchedvar)
{
    case 'foo';
        echo 'foo';
    break;
    case 'bar';
        echo 'bar';
    break;
    default;
        echo 'Not found';
    break;
}

Lots of Functions

Creating lots of functions can be an easy way out but it can introduce other bugs
if you are not careful with existing functions, namespaces etc.

Placing a prefix first can limit this effect.

function foostuff_foo(){
    echo 'foo';
}
function foostuff_bar(){
    echo 'bar';
}
//Then to use it
$switchedvar = 'foo';
$function = "foostuff_{$switchedvar}";
if(function_exists($function)) {
    $function() ;
} else {
    echo "Not found";
}

Class Functions

To limit the existing functions issues from the previous example, you can create a simple class with all the items of the switch in it.

//Normal Functions
class FooStuff
{
   function foo(){
        echo 'foo';
   }
   function bar(){
        echo 'bar';
   }
}

//Then to use it
$switchedvar = 'foo';
$stuff = new foostuff();
if(is_callable(array($stuff, $switchedvar))) {
    $stuff->{$switchedvar}() ;
} else {
    echo "Not found";
}
//Static functions
class FooStuff
{
   static function foo(){
        echo 'foo';
   }
   static function bar(){
        echo 'bar';
   }
}

//Then to use it
$switchedvar = 'foo';
if(is_callable(array('\FooStuff', $switchedvar))) {
    FooStuff::{$switchedvar}() ;
} else {
    echo "Not found";
}

Switching Class

If its worth doing, its worth over doing….
The class code for SwitchFunction.php and SwitchStatic.php I have moved to their own pages, due to their size.
I do use this sometimes via Composer, but it is not public currently.
It is also noticable that this is getting close to a cheep controller in a MVC architectural.

require "SwitchFunction.php";
class BarStuff extends SwitchIt\SwitchFunction {
	public function foo($data) {
		echo "foo \n";
	}
	public function bar($data) {
		echo "bar \n";
	}
	public function DefaultSwitch($data) {
		echo "default \n";
	}
}
require "SwitchStatic.php";
class FooStuff extends SwitchIt\SwitchStatic {
	static function foo($data) {
		echo "foo \n";
	}
	static function bar($data) {
		echo "bar \n";
	}
	static function DefaultSwitch($data) {
		echo "default \n";
	}
}

Then to use the classes.

$bar = new BarStuff ();
$bar->SwitchIt ( 'bar' );
$bar->SwitchIt ( 'barfoo' );
//Or
new BarStuff ('foo');
FooStuff::SwitchIt ( 'foo' );
FooStuff::SwitchIt ( 'foobar' );

Conclusion

No conclusions really, it most likely won’t be as fast as a generic switch statement and more likely annoy a lot of people.

Sponsor

About Chris Chase 16 Articles
Yeah i could write something here... but no that's not going to happen.

Be the first to comment

Leave a Reply

Your email address will not be published.


*