Last active
November 13, 2016 03:10
-
-
Save rc1021/5ca040a18ddef0c62407bd1010e21cee to your computer and use it in GitHub Desktop.
brief switch in laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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...'); | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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