I got a tiny question for which I could not find any proper answer by
simply googling or the likes.
I am not quite sure what might be the best way to initialize my class
elements. For now I initialize them with values of the type they
actually should contain. Example:
private $_users = array();
private $_admin_name = '';
private $_max_users = 0;
private $_user_model = null;
I thought this would be a good way but I found it a general source of
some errors in my code when I want to use those elements in
comparisons. When I pass them though many methods and other classes at
some point I maybe won't remember what the type of this variable was.
And since I like to do strict comparison (===, !==) it can make my
conditions fail even they should not. And please do not encourage me
to use hungarion notation. That is really ugly to me.
So I just want to know what your general approach und initializing
class elements is and why you prefer your way over the others.
Thanks in advance
Benjamin
No problem with the way you initialize them - but you should ALWAYS
remember what the type is. It's much easier if you document your class
before writing the code.
> So I just want to know what your general approach und initializing
> class elements is and why you prefer your way over the others.
>
> Thanks in advance
> Benjamin
No 100% rule, but in general:
1) Initialize array members to empty arrays,
2) Initialize elements with valid defaults to those default values
3) Initialize other elements to a value indicating it's invalid (often
times null).
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstu...@attglobal.net
==================
Well it seems to me that this has nothing whatsoever to do with
initialization.
Regardless as to whether the variables have their initial or a
subsequent value, if you want to make strict comparisons, then you
have to know what types you are dealing with.
So you have some choices:
1) Use Hungarian Notation to help you keep track of the types
2) Keep a data dictionary to hand so that you can easily look up the
type
3) Stop using strict comparisons