PHP & Zend Engine (50 Qs)•Part 1: Core Language, Execution Lifecycle & Types (1 - 10)
Explain the PHP Type System: Union Types, Intersection Types, and DNF Types?
junior LevelCode ExampleLifecycle
In Simple Words:
- Union Types (
A|B): Accepts either type A or type B. - Intersection Types (
A&B): Accepts a value that must implement both type A and type B simultaneously. - DNF Types (
(A&B)|C): Disjunctive Normal Form allows combining intersection types wrapped in unions.
php
// DNF Type: Accepts either a Loggable entity with ArrayAccess OR null:
function process((Loggable & ArrayAccess) | null $data): void {
if ($data !== null) {
$data->log();
}
}