rick645 Posted October 16, 2023 Share Posted October 16, 2023 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 Quote Link to comment Share on other sites More sharing options...
requinix Posted October 16, 2023 Share Posted October 16, 2023 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? Quote Link to comment Share on other sites More sharing options...
rick645 Posted October 17, 2023 Author Share Posted October 17, 2023 https://www.amitmerchant.com/reasons-use-composition-over-inheritance-php/ ok maybe it's right, in my case, to use inheritance Quote Link to comment Share on other sites More sharing options...
Solution requinix Posted October 17, 2023 Solution Share Posted October 17, 2023 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.