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 ...

Get The Art of Modern PHP 8 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.