Yii, MVC, views and strict error reporting

Hi there,

As I pointed out in one of discussions, turning on E_STRICT error reporting in php.ini is one of the first things I do for each larger project, I develop. This sometimes might be painful for the developer, I mean - especially all those reminders about unset variable, which become fatal errors and are halting application execution, when E_STRICT is on.

The easiest way to avoid them is of course to use isset function. But what about rendering a views, where you are passing some variables as arrays? What to do there in this error reporting mode? I would like to ask you, what in your opinion is best approach for this problem.

Suppose we have:


$this->render('index', array

(

    	'scripts_array'=>$scripts_array,

    	'flash'=>$flash,

));

and suppose one or both of this additional variables might not be set in some situations as, for example they are result of if condition check or for loop.

I know two approaches for solving this problem. One approach is to force setting of variable before passing it to view. Like this:


$scripts_array = (isset($scripts_array)) ? $scripts_array : '';

$flash = (isset($flash)) ? $flash : '';


$this->render('index', array

(

    	'scripts_array'=>$scripts_array,

    	'flash'=>$flash,

));

Second one is to use variable as array when passing stuff to view. Like this:


$array = (isset($scripts_array)) ? array($scripts_array) : array();

$array = (isset($flash)) ? array_merge($array, $flash) : $array;


$this->render('index', array($array));

Which approach is better in your opinion? Or maybe you know a third or fourth, even better solution?

Maybe because of my background as a C programmer… I like to inizialize the variables…

like




$var='';  //.. 0 or array() - depending of the need

if(...)

   $var=<expression>;



As for the E_STRICT on my development computer I turn that ON too together with logging and html_errors…

I think that is better to pass always the same parameters to the view, so better to initialize variables and pass, if necessary, a null.

I too like E_STRICT, it hepls to find the typos, a kind of arror I am really fan of.

If I’m not mistaken, typos (i.e. printr or pirnt_r instead of print_r) should be always reported by PHP, no matter what level of error reporting you have set. As they are critical errors, that make unable to continue with script execution.

Or maybe you are talking about typos in variable names, like $feel_grid instead of $fill_grid? :]

Yes, I am talking about typos in variable.

With e-notice you are foced to typo another existing variable (stuff that sometimes happens to me, but more seldom)