开发者

how can i make variable declaration optional in codeigniter?

coming from a modular php programming environment i got used to using variables in php without regard of wether they were instantiated or not.

i've been using codeigniter for around a month now and noticed that it is strictly implementing on not using a variable if they are undefined / undeclared.

i would like to know what's the purpose behind this and how does it differ from not really being strict ab开发者_如何学Cout undeclared variables?

is there a way that i can disable this feature in codeigniter in case i prefer to stick to using undeclared variables? and if so, what are the cons i'd get?


A big reason to initialize all variables is security. It really matters if you have register_globals on (which you shouldn't). But even apart from that, it's good style and aids debugging if your variables are always in a defined and known state. If you get into the habit of always initializing your variables and suddenly get a warning about using an uninitialized variable, you know it's due to a typo, and not one of a gazillion other reasons. Another reason is variable recycling in longer functions (which you should avoid anyway, but can get really messy if you don't reinitialize the variable).

Since using uninitialized variables is a language "feature", it's still available in CodeIgniter. CI might set the default error reporting level higher than you're used to, which probably results in more visible warnings than before.


If that's not what you meant, please clarify your question.


In you index.php file at the root of your project set this:

error_reporting(E_ALL);

to:

error_reporting(E_ALL  & ~E_NOTICE);

This setting does not show E_NOTICE level errors

for more info check out http://uk.php.net/manual/en/errorfunc.configuration.php#ini.error-reporting


The con to not initializing your variables is that a hidden E_NOTICE error occurs for each uninstantiated variable. This creates a fair amount of unnecessary overhead because the system must handle each instance of an E_ERROR, regardless of whether it is logged or turned off by error_reporting()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜