Re: Constructor as a "Reset" Button



On Jul 24, 5:03 pm, Sanders Kaufman <bu...@xxxxxxxxxxx> wrote:
Michael Fesser wrote:
.oO(Sanders Kaufman)
That's ONE use for the constructor.

It's the only use. As it's name suggests - it's used to create an object
(and a destructor is called while cleaning up, respectively). In PHP the
constructor is more or less just for initialization, in other languages
it's also used to allocate memory for the new object.

That was funny.
It took just two sentences for you to contradict yourself.

You're somewhat correct when you say that it's "more or less just for
initialization". It is indeed useful for MORE than just initializing
the object. In fact, it's also pretty good at re-initializing the object.


So are you suggesting that you call the constructor from elsewhere in
the class (or even explicitly from outside the class)? That's just
poor form. The constructor should be used for one thing only:
constructing a new instance of a class.

If you want to "reset" the object, then you separate that out into its
own method and call it from the constructor -- similar to what you've
done with the Connect() method.

I'm firmly with Micha on this one.

.



Relevant Pages

  • Re: Constructor as a "Reset" Button
    ... constructor is more or less just for initialization, ... You're somewhat correct when you say that it's "more or less just for initialization". ... In PHP you might get ... I am, however, simultaneously developing a reference model for this foundation/framework and it IS a global application object. ...
    (comp.lang.php)
  • Re: Message Builder vs. a Build Method?
    ... Builder class would encapsulate all the complex algorithms for making ... OTOH, as Daniel T. suggests, sometimes the initialization requires unique processing for initialization that is clearly intrinsic to the object itself. ... Constructors tend to be fragile and it is difficult to manage errors when they occur in a constructor scope, so it is usually a good idea to keep the processing in constructors as simple as possible. ... When the initialization of attribute data requires complex processing AND it seems like is intrinsic ot the object, that justifies having a separate initialization method that is invoked immediately after the constructor. ...
    (comp.object)
  • Re: Localization Service
    ... these assets are contents to be edited by the users. ... problem with a validatorFactory. ... I was filling the dictionary on the constructor. ... beware of too much deferred initialization. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Constructor as a "Reset" Button
    ... constructor is more or less just for initialization, ... In PHP you might get ... And HIPPA certification is not easy - nor is it cheap. ...
    (comp.lang.php)
  • Re: simple question
    ... I mean it will be just the same if I remove the c-tor and change this row ... member field initializers are in fact handled differently from initialization in the constructor. ... Rather, a more explicit initialization is constructed from your declarations, that looks something like the following. ...
    (microsoft.public.dotnet.languages.csharp)