Initializing as type array
Posted: Wed Jul 11, 2007 10:02 pm
I've always initialized members as NULL and then cast their type as appropriate or as required.
I've been running some code through some last minute checks and found that my code was occasionally throwing an error when array_values and similar functions were operating on members which hadn't been initialized (by design - that is they are not intended to be so they are NULL) and because they were set to NULL the error was thrown.
I just tried initializing all my array members as type array()
Instead of
And that seems to have done the trick and I was able to remove error suppression operators in favor of much cleaner code. 
How do you deal with these situations? Do you initialize the the expected type or just use error suppression or possible conditionals at the point of error? Is there a better way?
I've been running some code through some last minute checks and found that my code was occasionally throwing an error when array_values and similar functions were operating on members which hadn't been initialized (by design - that is they are not intended to be so they are NULL) and because they were set to NULL the error was thrown.
I just tried initializing all my array members as type array()
Code: Select all
$_member = array()Code: Select all
$_member = null;How do you deal with these situations? Do you initialize the the expected type or just use error suppression or possible conditionals at the point of error? Is there a better way?