0
0
PHPprogramming~15 mins

Intersection types in PHP - Deep Dive

Choose your learning style9 modes available
Overview - Intersection types
What is it?
Intersection types in PHP allow a variable or parameter to be declared as requiring multiple types at the same time. This means the value must satisfy all the listed types simultaneously. It helps make code more precise by combining multiple type requirements into one declaration.
Why it matters
Without intersection types, developers often rely on loose type checks or comments to explain complex type requirements, which can lead to bugs and unclear code. Intersection types enforce stricter rules at the language level, preventing errors early and making code easier to understand and maintain.
Where it fits
Before learning intersection types, you should understand basic PHP types, union types, and type declarations. After mastering intersection types, you can explore advanced type features like generics (in other languages), or design patterns that leverage strict typing for safer code.
Mental Model
Core Idea
An intersection type means a value must be all the specified types at once, like wearing multiple hats simultaneously.
Think of it like...
Imagine a person who must be both a teacher and a musician to join a special club. They need to have skills from both roles, not just one or the other.
Value
  │
  ├─ Must be Type A
  ├─ Must be Type B
  └─ Must be Type C

Only values that satisfy all these types pass.
Build-Up - 6 Steps
1
FoundationBasic PHP type declarations
🤔
Concept: Learn how PHP declares types for variables and function parameters.
PHP allows you to specify types like int, string, or classes for function parameters and return values. For example: function greet(string $name): string { return "Hello, $name!"; } This ensures only strings are accepted and returned.
Result
The function only accepts strings and returns a string.
Understanding simple type declarations is essential before combining multiple types.
2
FoundationIntroduction to union types
🤔
Concept: Union types allow a value to be one of several types, increasing flexibility.
PHP 8.0 introduced union types, letting you declare a parameter as accepting multiple types, like: function process(int|string $value) { // $value can be int or string } This means the value can be either type, not both.
Result
The function accepts either an int or a string.
Union types show how PHP can accept multiple types, but they differ from intersection types.
3
IntermediateUnderstanding intersection types syntax
🤔Before reading on: do you think intersection types accept values matching any one type or all types? Commit to your answer.
Concept: Intersection types require a value to satisfy all listed types simultaneously, using the '&' symbol.
PHP 8.1 introduced intersection types using '&'. For example: function example(A&B $value) { // $value must be both A and B } This means $value must be an object that implements both interfaces or classes A and B.
Result
Only values that are both A and B are accepted.
Knowing that '&' means 'and' clarifies that intersection types are stricter than unions.
4
IntermediatePractical use with interfaces
🤔Before reading on: can a class implement multiple interfaces to satisfy an intersection type? Commit to your answer.
Concept: Intersection types are often used with interfaces to require multiple behaviors at once.
If you have interfaces Logger and Serializer, you can require a parameter to be both: function handle(Logger&Serializer $obj) { $obj->log('start'); $obj->serialize(); } A class implementing both interfaces can be passed here.
Result
The function safely calls methods from both interfaces.
Understanding intersection types helps enforce multiple capabilities in one object.
5
AdvancedCombining intersection and union types
🤔Before reading on: do you think you can mix '&' and '|' in the same type declaration? Commit to your answer.
Concept: PHP allows combining intersection and union types with parentheses to clarify precedence.
Example: function test((A&B)|C $value) { // $value is either both A and B, or C } Parentheses group intersection before union.
Result
The function accepts values matching either condition.
Knowing how to combine these types allows expressing complex type rules precisely.
6
ExpertLimitations and runtime behavior
🤔Before reading on: do you think intersection types are checked at runtime or only at compile time? Commit to your answer.
Concept: Intersection types are enforced at runtime by PHP's type system, but some limitations exist with traits and classes.
PHP checks intersection types when functions are called. However, traits cannot be used directly in intersection types, and some complex inheritance can cause unexpected errors. Example: class A {} class B {} function foo(A&B $obj) {} This requires $obj to be instance of a class extending both A and B, which is impossible, so intersection types are mainly for interfaces.
Result
Runtime errors occur if types don't match exactly.
Understanding runtime checks and limitations prevents misuse and bugs in production.
Under the Hood
PHP's engine checks type declarations at runtime when functions are called or values assigned. For intersection types, it verifies the value implements or extends all specified types. This involves checking the object's class hierarchy and interfaces. If any type is missing, a TypeError is thrown immediately.
Why designed this way?
Intersection types were introduced to improve type safety and expressiveness without breaking backward compatibility. Using runtime checks fits PHP's dynamic nature, allowing gradual typing. The design balances strictness with flexibility, avoiding complex compile-time checks that PHP does not perform.
Call function with value
      │
      ▼
  Check if value is instance of Type1
      │
      ├─ No → Throw TypeError
      │
      ▼
  Check if value is instance of Type2
      │
      ├─ No → Throw TypeError
      │
      ▼
  ... (all types checked)
      │
      ▼
  Accept value and run function
Myth Busters - 4 Common Misconceptions
Quick: Does an intersection type accept values matching any one of the types or all of them? Commit to your answer.
Common Belief:Intersection types accept values that match any one of the listed types, like union types.
Tap to reveal reality
Reality:Intersection types require the value to match all listed types simultaneously, not just one.
Why it matters:Confusing intersection with union types leads to passing incompatible values, causing runtime errors.
Quick: Can intersection types be used with classes that do not share inheritance? Commit to your answer.
Common Belief:You can intersect any classes regardless of inheritance hierarchy.
Tap to reveal reality
Reality:Intersection types with classes require the value to be an instance of a class that extends all intersected classes, which is impossible unless one class extends the other. Intersection types are mainly useful with interfaces.
Why it matters:Trying to intersect unrelated classes causes impossible type requirements and errors.
Quick: Are intersection types checked at compile time or runtime? Commit to your answer.
Common Belief:Intersection types are checked at compile time like in some statically typed languages.
Tap to reveal reality
Reality:PHP checks intersection types at runtime when the function is called or value assigned.
Why it matters:Expecting compile-time errors can mislead debugging and testing strategies.
Quick: Can traits be used directly in intersection types? Commit to your answer.
Common Belief:Traits can be used in intersection types just like interfaces or classes.
Tap to reveal reality
Reality:Traits cannot be used in intersection types because they are not types themselves but code reuse mechanisms.
Why it matters:Misusing traits in intersection types causes syntax errors and confusion.
Expert Zone
1
Intersection types cannot include traits because traits are not types but code reuse tools, which often surprises developers.
2
Combining intersection and union types requires careful use of parentheses to avoid unexpected precedence issues.
3
Runtime checks for intersection types can impact performance slightly, so they should be used judiciously in hot code paths.
When NOT to use
Avoid intersection types when dealing with classes that do not share inheritance, as this creates impossible type requirements. Instead, use interfaces or rethink design. Also, if you need to accept multiple alternative types, union types are better suited.
Production Patterns
In production, intersection types are commonly used to enforce multiple interface implementations for dependency injection, ensuring objects have all required capabilities. They also help in APIs to guarantee strict contracts, reducing bugs caused by missing methods.
Connections
Set theory
Intersection types correspond to the intersection operation in set theory, where elements must belong to all sets simultaneously.
Understanding set intersections clarifies why intersection types require all conditions to be met, not just one.
Multiple inheritance (OOP)
Intersection types relate to multiple inheritance by requiring an object to behave like multiple types at once, similar to inheriting from multiple classes or interfaces.
Knowing multiple inheritance helps understand how intersection types enforce multiple behaviors in one object.
Logical AND operator in Boolean logic
Intersection types act like the logical AND operator, requiring all conditions (types) to be true (satisfied).
Recognizing this logical connection helps grasp the strictness of intersection types compared to union types (logical OR).
Common Pitfalls
#1Trying to use intersection types with unrelated classes causing impossible type requirements.
Wrong approach:function foo(A&B $obj) {} class A {} class B {} $obj = new B(); foo($obj);
Correct approach:interface A {} interface B {} class C implements A, B {} function foo(A&B $obj) {} $obj = new C(); foo($obj);
Root cause:Misunderstanding that intersection types require a single value to satisfy all types, which is impossible if classes are unrelated.
#2Confusing intersection types with union types and expecting values to match any one type.
Wrong approach:function bar(A&B $obj) {} // Passing an object of only A or only B $objA = new A(); bar($objA); // Expecting this to work
Correct approach:function bar(A|B $obj) {} $objA = new A(); bar($objA); // Works because union accepts either type
Root cause:Not recognizing that intersection types require all types simultaneously, unlike union types.
#3Attempting to use traits in intersection types causing syntax errors.
Wrong approach:function baz(TraitA&TraitB $obj) {}
Correct approach:Use interfaces or classes instead: interface InterfaceA {} interface InterfaceB {} function baz(InterfaceA&InterfaceB $obj) {}
Root cause:Confusing traits (code reuse) with types (interfaces/classes) that can be used in type declarations.
Key Takeaways
Intersection types require a value to satisfy all specified types simultaneously, making type declarations more precise.
They are mainly useful with interfaces because intersecting unrelated classes is usually impossible.
Intersection types use the '&' symbol and can be combined with union types using parentheses for complex rules.
PHP enforces intersection types at runtime, throwing errors if the value does not meet all type requirements.
Understanding intersection types improves code safety by ensuring objects have multiple required behaviors.