0
0
PHPprogramming~5 mins

Intersection types in practice in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an intersection type in PHP?
An intersection type requires a value to satisfy multiple type constraints at the same time. It means the value must be an instance of all specified types.
Click to reveal answer
beginner
How do you declare an intersection type in PHP 8.1+?
You use the ampersand (&) symbol between types, for example: <code>function example(SomeInterface&AnotherInterface $obj) {}</code> means $obj must implement both interfaces.
Click to reveal answer
intermediate
Why use intersection types in practice?
They help ensure that a value meets multiple requirements, improving code safety and clarity by enforcing multiple behaviors or properties at once.
Click to reveal answer
intermediate
Can intersection types be used with classes and interfaces together?
Yes. For example, ClassA&InterfaceB means the value must be an instance of ClassA and also implement InterfaceB.
Click to reveal answer
beginner
What happens if a value does not satisfy all types in an intersection type declaration?
PHP will throw a TypeError at runtime, indicating the value does not meet the required intersection type constraints.
Click to reveal answer
Which symbol is used to declare intersection types in PHP?
A,
B|
C&
D:
What does the intersection type A&B mean in PHP?
AValue must be both A and B
BValue must be A but not B
CValue must be neither A nor B
DValue must be either A or B
Can intersection types include multiple interfaces?
AOnly traits can be combined
BNo, only classes
COnly one interface is allowed
DYes, multiple interfaces can be combined
What error occurs if a value does not meet an intersection type requirement?
AParseError
BTypeError
CWarning
DNotice
Which PHP version introduced intersection types?
APHP 8.1
BPHP 8.0
CPHP 7.4
DPHP 8.2
Explain how intersection types improve code safety in PHP.
Think about how requiring multiple interfaces or classes helps avoid wrong values.
You got /4 concepts.
    Describe how to declare and use an intersection type with a class and an interface in PHP.
    Show a function parameter that requires an object to be both a class instance and implement an interface.
    You got /4 concepts.