Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Initialize class elements: Yes/No, which value?

0 views
Skip to first unread message

benjamin.s...@googlemail.com

unread,
Jan 3, 2010, 9:38:46 PM1/3/10
to
Hello everybody,

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

Jerry Stuckle

unread,
Jan 3, 2010, 9:53:19 PM1/3/10
to
benjamin.s...@googlemail.com wrote:
> Hello everybody,
>
> 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.
>

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
==================

Captain Paralytic

unread,
Jan 4, 2010, 7:19:16 AM1/4/10
to
On Jan 4, 2:38 am, "benjamin.schneider...@googlemail.com"

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

0 new messages