Jump to content

Avoid inheritance with exceptions


rick645
Go to solution Solved by requinix,

Recommended Posts

Many say they avoid inheritance and to encourage composition.
But with the exceptions, how do you do it?

The following example, how would you modify it to eliminate the smell codes?

new class('OTHER', 'MESSAGE', 123) extends InvalidArgumentException {// SMELL CODE
    function __construct(private $otherDetails, ...$params)
    {
        parent::__construct(...$params);// SMELL CODE
    }
};

Something similar could be tempted

new class('OTHER', new InvalidArgumentException('MESSAGE', 123)) implements InvalidArgumentException {
    function __construct(private $otherDetails, private InvalidArgumentException $wrapped)
    {
    }
};

but there would be problems already in the compilation phase

PHP Fatal error:  InvalidArgumentException@anonymous cannot implement InvalidArgumentException - it is not an interface

 

Link to comment
Share on other sites

51 minutes ago, rick645 said:

Many say they avoid inheritance and to encourage composition.

But do you actually understand why they say that? Because they are not saying that inheritance is bad.

So tell me: in this situation you find yourself in, why do you feel that composition is better than inheritance?

Link to comment
Share on other sites

  • Solution
39 minutes ago, rick645 said:

ok maybe it's right, in my case, to use inheritance

Probably.

Inheritance represents an "is a" relationship. Would it be accurate to say that your custom exception class "is an" InvalidArgumentException? If I had code that caught InvalidArgumentException, should that code also be able to catch your exception?
Composition represents a "has a" relationship - or "needs a", or something else similar to that. Would it be accurate to say that your exception class "has an" InvalidArgumentException? Is your class a distinct form of exception separate, but not entirely unrelated to, an InvalidArgumentException?

The answer seems like it would be the first one, however the $otherDetails is suspicious and suggests something more than an invalid argument, thus perhaps composition is more appropriate, however the fact that you chose to make it anonymous means it will be impossible to catch or type guard for that particular class in the first place, which makes composition almost useless.

In other words, your example doesn't make sense. If you want a special exception class then it should be a full named class. If you want special exception data then it needs to be a full named class.

And that is the real code smell.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.