Chapter 6: Parameter, Property, and Return Types
Hopefully, after reading the preceding chapters and topping up your knowledge gaps by hitting the official docs pages, you now have a good understanding of how PHP types work. What we are going to explore in this chapter is the way that those types can be checked and enforced in function or method parameters and return types. This is where types really gain their utility and so it is important that you have a clear understanding of how this works.
We will start off by looking at the difference between the default "coercive" mode and the recommended but optional "strict" mode. Then, we are going to try to get our heads around covariance and contravariance and understand how they limit what we can do when extending parent classes or implementing interfaces.
This chapter will cover the following topics:
- Coercive and strict modes
- Covariance and contravariance
As usual, you can find all the code...