Skip to content

Instantly share code, notes, and snippets.

@rc1021
Last active November 13, 2016 03:10
Show Gist options
  • Select an option

  • Save rc1021/5ca040a18ddef0c62407bd1010e21cee to your computer and use it in GitHub Desktop.

Select an option

Save rc1021/5ca040a18ddef0c62407bd1010e21cee to your computer and use it in GitHub Desktop.
brief switch in laravel
<?php
// Hi, I am Charles<[email protected]>
// let's go...
namespace App;
use Exception;
class DemoClass
{
function demo1($do="")
{
// 以前的做法
$str = "result: ";
switch ($do) {
case 'commit':
$str .= "you seletor [commit] to do something...";
break;
case 'execute':
$str .= "you seletor [execute] to do something...";
break;
default:
$str .= "you seletor nothing...";
break;
}
return $str;
}
function demo2($do="")
{
// 但你可以這麼做,程式看起來會更漂亮
$str = "result: ";
return $str.array_get([
'commit' => 'you selector [commit] to do something...',
'execute' => 'you selector [execute] to do something...',
], $do, 'you seletor nothing...');
}
}
<?php
use App\DemoClass;
// --- demo1 ---
echo (new DemoClass)->demo1('execute'); // output -- result: you selector [execute] to do something...
// --- demo2 ---
echo (new DemoClass)->demo2('execute'); // output -- result: you selector [execute] to do something...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment