IMPORTANT: cakephp 使用一个变量前先用isset判断其是否存在

4 views
Skip to first unread message

志远 张

unread,
May 12, 2013, 12:51:01 AM5/12/13
to zh...@googlegroups.com
我看到我们得代码中有比较多类似于一下问题:

Notice (8): Undefined index: ext [APP/Controller/UsersController.php, line 156]
Code Context
UsersController::host_login() - APP/Controller/UsersController.php, line 156
ReflectionMethod::invokeArgs() - [internal], line ??
Controller::invokeAction() - CORE/Cake/Controller/Controller.php, line 486
Dispatcher::_invoke() - CORE/Cake/Routing/Dispatcher.php, line 187
Dispatcher::dispatch() - CORE/Cake/Routing/Dispatcher.php, line 162
[main] - APP/webroot/index.php, line 96

主要问题是在使用一个变量时候么有去判断该变量是否存在:
 if ($this->request->params['ext'] == 'json') {
在使用前没有判断就会有上面的问题,虽然在此不用判断逻辑最终也是对的,但是我们最好养成好习惯,在使用前判断其是否存在,虽然效率上会有点小小的折扣,但是对我们以后的扩展和维护特别有帮助,能减少很多不必要的麻烦。

改为:
 if (isset($this->request->params['ext']) && $this->request->params['ext'] == 'json') {
谢谢!
Regards
Derek
Reply all
Reply to author
Forward
0 new messages