0
0
PHPprogramming~10 mins

Intersection types in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Intersection types
Define Interfaces
Create Class implementing both
Use Intersection Type in Function
Pass Object to Function
Function accepts only if object matches ALL types
Intersection types require a value to satisfy multiple type constraints at once, like an object implementing all specified interfaces.
Execution Sample
PHP
<?php
interface A { function a(); }
interface B { function b(); }
class C implements A, B {
  function a() { echo "A"; }
  function b() { echo "B"; }
}
function test(A&B $obj) {
  $obj->a(); $obj->b();
}
$obj = new C();
test($obj);
?>
This code defines two interfaces and a class implementing both, then a function requiring an object that implements both interfaces (intersection type).
Execution Table
StepActionEvaluationResult
1Define interface Ainterface A with method a()Interface A created
2Define interface Binterface B with method b()Interface B created
3Define class C implements A and BClass C has methods a() and b()Class C created
4Create function test with parameter type A&BParameter must implement both A and BFunction test created
5Create object $obj = new C()Object $obj is instance of CObject $obj created
6Call test($obj)Check if $obj implements A and BPasses, function runs
7Inside test: call $obj->a()Calls method a()Outputs 'A'
8Inside test: call $obj->b()Calls method b()Outputs 'B'
9Function test endsAll calls successfulOutput: 'AB'
💡 Function test accepts only objects implementing both interfaces A and B; $obj meets this, so execution completes.
Variable Tracker
VariableStartAfter Step 5After Step 6Final
$objundefinedInstance of CPassed to test()Instance of C
Key Moments - 2 Insights
Why does the function parameter use A&B instead of just A or B?
Because the function requires an object that implements both interfaces A and B simultaneously, not just one. See execution_table step 4 and 6 where the intersection type enforces this.
What happens if we pass an object that implements only A or only B?
The function call will fail because the intersection type requires both. This is shown in execution_table step 6 where the check ensures the object implements both interfaces.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6, what is checked before the function runs?
AIf $obj implements both interfaces A and B
BIf $obj implements either interface A or B
CIf $obj is a subclass of C
DIf $obj is null
💡 Hint
Check the 'Evaluation' column at step 6 in execution_table.
At which step does the program output the letter 'B'?
AStep 7
BStep 8
CStep 6
DStep 9
💡 Hint
Look at the 'Action' and 'Result' columns for output in execution_table steps 7 and 8.
If class C did not implement interface B, what would happen at step 6?
AFunction test would throw an error at step 7
BFunction test would accept $obj anyway
CFunction test would reject $obj due to missing interface B
DNothing would change
💡 Hint
Refer to the intersection type requirement in execution_table step 4 and 6.
Concept Snapshot
Intersection types in PHP require a value to satisfy multiple types at once.
Syntax: function f(A&B $obj) means $obj must implement both A and B.
Useful for enforcing multiple interface implementations.
If the object misses any type, PHP throws a TypeError.
Allows safer, clearer code contracts.
Full Transcript
This example shows how PHP intersection types work by defining two interfaces A and B, and a class C that implements both. A function test requires an argument that implements both interfaces, using the syntax A&B. When an object of class C is passed, the function calls methods from both interfaces successfully, outputting 'AB'. The execution table traces each step from defining interfaces and class, creating the object, checking the intersection type, and calling methods. Key moments clarify why both interfaces are required and what happens if an object does not meet the intersection. The visual quiz tests understanding of type checks and output steps. Intersection types help ensure objects meet multiple type requirements simultaneously, improving code safety and clarity.