46

I know I can throw exception from constructor in PHP but should I do it? For example, if a parameter's value is not as I expected it.

Or should I defer throwing an exception till a method is invoked. What are advantages and disadvantages in both cases?

  • the question is do you want your users to see ugly exception error messages? – Lawrence Cherone Feb 29 '12 at 10:57
  • 15
    @LawrenceCherone no, that's not the question. Not at all. The question is whether to raise an exception immediately when the ctor cannot set the object into a valid state from the arguments passed to it or to defer that until a method operating on that instance with possible invalid state is called. Better suited for programmers though. – Gordon Feb 29 '12 at 11:00
  • 4
    @LawrenceCherone You're right. No software should ever deliberately fail ever; it should just keep going and see what happens. /s – user253751 Feb 18 '16 at 23:39
  • @immibis Good sarcasm is good. – kodeart Jan 2 '17 at 14:01
  • This is an awesome question!!! I never even considered this idea until today. – Rhys Johns Jun 26 '17 at 23:01
75

Why would you postpone throwing the exception?

If you know that the object can't properly instantiate with the given parameters, then you should definitely throw an exception.

Otherwise, somebody might test your object for null, which it won't be, and could assume everything went as expected.

There are a lot of things that can be done to your object without calling a method on it: it could be added to a list, it could be compared, it could be sent as a parameter, etc etc etc. All of these are things that should not have happened, considering it is not a valid object.

21

If your constructor is invoked with bad parameter(s) then you should (must?) throw an exception. If you do not then you could get a bad object which will not behave as expected.

17

Absolutely!!

You should throw the exception if the parameters for constructing the object is not valid or not as per the contract. It is not a good idea to go ahead with the flow knowing that the object is constructed with bad data which can cause many issues if you allow the caller to go ahead.

It is always better to "FAIL FAST AND FAIL EARLY"

7

I know I can throw exception from constructor in PHP but should I do it?

That is the only sane way to inform that the object construction failed.

2

Why would you not validate the parameter set prior to instantiating your object? Doing so would ensure that your object would be created, thus, removing any of the side affects that may occur from its failure.

Though I know you can check things in your constructor and toss exceptions, I prefer to write my constructors in such a way that they do not fail. I perform parameter validation before instantiating objects which I can then toss exceptions without having my constructors fail. I also generally do not try to instantiate new objects in my constructors, choosing instead to instantiate them as I need them.

Just my opinion. PHP offers a lot of freedom - enjoy!

  • I'm not sure why this answer is downvoted. It provides a valid approach using parameter validation prior the object construction (a "guard" method) which is a good way to design your classes/functions for specific (usually business) rules. If also does not answer directly the OP's question, but it's still a good advice :) – kodeart Jan 2 '17 at 13:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.