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.
Example:
To give you an idea how I usually use this approach:
new Email('to@example.com', 'Subject line', 'Body Text');
I avoid to overuse it, of course, but in my opinion, this is really handy.
if the code in the constructor is part of creating and initializing the object for use, then I would put it there, but thats me personally, some people may disagree
however, it looks like what you are doing is not intended for building the object/class but doing some other process. this is bad, and should be done in a separate method.
Keep the constructor for construction.