Is there anything wrong with taking immediate actions in constructors?

Posted by pestaa on Stack Overflow See other posts from Stack Overflow or by pestaa
Published on 2010-05-19T20:20:07Z Indexed on 2010/05/19 20:30 UTC
Read the original article Hit count: 148

I have classes like this one:

class SomeObject
{
    public function __construct($param1, $param2)
    {
       $this->process($param1, $param2);
    }
    ...
}

So I can instantly "call" it as some sort of global function just like

new SomeObject($arg1, $arg2);

which has the benefits of

  • staying concise,
  • being easy to understand,

but might break unwritten rules of semantics by not waiting till a method is called.

Should I continue to feel bad because of a bad practice, or there's really nothing to worry about?

Clarification:

  • I do want an instance of the class.
  • I do use internal methods of the class only.
  • I initialize the object in the constructor, but call the "important" action-taker methods too.
  • I am selfish in the light of these sentences.

© Stack Overflow or respective owner

Related posts about best-practices

Related posts about language-agnostic