Laravel0Composition over inheritance in final classes

[ad_1]

Final classes, you either love them or hate them. People have been using them more recently in their open-source packages, but what does that mean for you? How do you handle this in application code?

Developers have been discussing composition over inheritance for years, and final classes are a perfect use case. A recent example I was talking to someone about was ‘moneyphp/money’, which implemented final classes. Let’s dive in.

Let’s take the example of ‘moneyphp/money’ and look at how we might integrate with it. Before final classes, we would extend the Money class and use it as I need to. However, that is not possible anymore, so we want to find a way to work with it. We will create a class called MoneyImplementation which will be the class we want to use.

1class MoneyImplementation

2{

3 private Money $money;

4 

5 public function __construct(

6 int|string $amount,

7 Currency $currency,

8 ) {

9 $this->money = new Money(

10 amount: $amount,

11 currency: $currency,

12 );

13 }

14}

So in the code above, we have used composition to build a class that will proxy its construction to the money class – setting a property on the class to the constructed instance. The next problem is how do we call class methods on the money class without extending the API we want to call. Without adding this, we would have to add an accessor to get the money instance from our class, and then use the accessor like the following.

1$money = new MoneyImplementation(

2 amount: 10_000,

3 currency: new Currency(

4 code: 'USD',

5 ),

6);

7 

8$money->money()->getAmount();

This isn’t ideal, but without any additional work, this is acceptable. But we can take it one step further using a little PHP magic. Let’s add this magic method to our MoneyImplementation class.

1class MoneyImplementation

2{

3 private Money $money;

4 

5 public function __construct(

6 int|string $amount,

7 Currency $currency,

8 ) {

9 $this->money = new Money(

10 amount: $amount,

11 currency: $currency,

12 );

13 }

14 

15 public function __call(string $name, array $arguments)

16 {

17 if (! method_exists($this->money, $name)) {

18 throw new RuntimeException(

19 message: "Method [$name] does not exist.",

20 );

21 }

22 

23 return $this->money->{$name}(...$arguments);

24 }

25}

We add a method __call to our class so we can proxy the call straight to the money instance we have already constructed. Before that, however, we can add a safety check to ensure the method exists. Using this, we can now simplify the API.

1$money = new MoneyImplementation(

2 amount: 10_000,

3 currency: new Currency(

4 code: 'USD',

5 ),

6);

7 

8$money->getAmount();

This may not be the best example to illustrate the point as we haven’t added anything to our class. However, it illustrates how we can use composition over inheritance to get around final package classes.

Have you found a workaround for this? How would you handle this scenario? Let us know on Twitter.

[ad_2]

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *