0
0
PHPprogramming~10 mins

Intersection types in practice in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Intersection types in practice
Define Interfaces
Create Class implementing both
Function requires intersection type
Pass object to function
Function uses methods from both interfaces
Output result
This flow shows how PHP uses intersection types to require an object to implement multiple interfaces before passing it to a function.
Execution Sample
PHP
<?php
interface A { function foo(): string; }
interface B { function bar(): string; }
class C implements A, B {
  function foo(): string { return "foo"; }
  function bar(): string { return "bar"; }
}
function test(A&B $obj): string {
  return $obj->foo() . " & " . $obj->bar();
}
$obj = new C();
echo test($obj);
This code defines two interfaces, a class implementing both, and a function requiring an intersection type to call methods from both interfaces.
Execution Table
StepActionEvaluationResult
1Define interface A with method foo()Interface A createdInterface A available
2Define interface B with method bar()Interface B createdInterface B available
3Define class C implementing A and BClass C created with foo() and bar()Class C available
4Create object $obj = new C()Object $obj createdInstance of C with foo() and bar()
5Call test($obj) with intersection type A&BCheck if $obj implements A and BTrue, proceed
6Inside test(), call $obj->foo()Call foo() method"foo" returned
7Inside test(), call $obj->bar()Call bar() method"bar" returned
8Concatenate results "foo & bar"Combine strings"foo & bar"
9echo test($obj)Output to screenfoo & bar
10End of scriptNo more codeExecution stops
💡 Script ends after outputting 'foo & bar'
Variable Tracker
VariableStartAfter Step 4After Step 5After Step 8Final
$objundefinedInstance of CInstance of CInstance of CInstance of C
Return value of test()undefinedundefinedundefined"foo & bar""foo & bar"
Key Moments - 3 Insights
Why does the function test() require the parameter type A&B instead of just A or B?
Because test() uses methods from both interfaces A and B, the parameter must implement both. The intersection type A&B ensures the object has all required methods, as shown in execution_table step 5.
What happens if we pass an object that implements only interface A to test()?
The type check in step 5 fails because the object does not implement B. PHP will throw a TypeError and stop execution before calling foo() or bar().
How does the class C satisfy the intersection type A&B?
Class C implements both interfaces A and B by defining both foo() and bar() methods, so an instance of C can be passed to test() as shown in steps 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6, what method is called on $obj?
Abar()
Bfoo()
Ctest()
DNone
💡 Hint
Check the 'Action' and 'Evaluation' columns at step 6 in the execution_table.
At which step does the function test() confirm that $obj implements both interfaces A and B?
AStep 7
BStep 4
CStep 5
DStep 9
💡 Hint
Look for the step where the intersection type check happens in the execution_table.
If class C did not implement interface B, what would happen at step 5?
APHP would throw a TypeError
BThe method foo() would not be called
CThe function test() would run normally
DThe script would output 'foo & bar'
💡 Hint
Refer to key_moments about what happens if the object does not satisfy the intersection type.
Concept Snapshot
Intersection types in PHP require an object to implement multiple interfaces.
Syntax: function f(A&B $obj) requires $obj to implement both A and B.
Use when a function needs methods from multiple interfaces.
If object lacks any interface, PHP throws a TypeError.
Class can implement multiple interfaces to satisfy intersection types.
Full Transcript
This example shows how PHP uses intersection types to require an object to implement multiple interfaces before passing it to a function. Two interfaces A and B are defined, each with one method. Class C implements both interfaces by defining both methods. The function test() requires a parameter that implements both A and B, using the intersection type A&B. When an instance of C is passed to test(), it calls both methods foo() and bar() and concatenates their results. The execution table traces each step from defining interfaces and class, creating the object, checking the intersection type, calling methods, and outputting the result. Key moments clarify why intersection types are needed and what happens if the object does not satisfy them. The visual quiz tests understanding of method calls, type checks, and error conditions. The concept snapshot summarizes the syntax and behavior of intersection types in PHP.