Skip to content

Instantly share code, notes, and snippets.

@sarciszewski
Created January 16, 2015 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sarciszewski/690609a0f8308f0aabd2 to your computer and use it in GitHub Desktop.
Save sarciszewski/690609a0f8308f0aabd2 to your computer and use it in GitHub Desktop.
Anti-Patterns
<?php
class switchNotMatched { }
function doInteger($i) {
echo $i;
if ($i < 100) {
echo " is less than 100.";
}
echo "\n";
}
function doString($i) {
echo str_rot13($i)."\n";
}
function doSomething($input) {
static $nomatch = null;
if (empty($nomatch)) {
$nomatch = new SwitchNotMatched();
}
switch($input) {
case preg_match('/^[0-9]+$/', $input)?$input:$nomatch:
doInteger($input);
break;
case preg_match('/^[a-zA-Z\s]+$/', $input)?$input:$nomatch:
doString($input);
break;
default:
echo "Something else...\n";
}
}
doSomething(10);
doSomething('101');
doSomething('lbh whfg ybfg gur tnzr');
doSomething('x7');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment