0
0
PHPprogramming~10 mins

Intersection types in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a function that accepts a parameter which must implement both interfaces A and B.

PHP
<?php
interface A {}
interface B {}

function test([1] $param) {
    // function body
}
?>
Drag options to blanks, or click blank then click option'
AA or B
BA|B
CA,B
DA&B
Attempts:
3 left
💡 Hint
Common Mistakes
Using | instead of & which means union type, not intersection.
Separating interfaces with commas or words like 'or'.
2fill in blank
medium

Complete the code to declare a variable with an intersection type of interfaces X and Y.

PHP
<?php
interface X {}
interface Y {}

/** @var [1] */
$obj;
?>
Drag options to blanks, or click blank then click option'
AX|Y
BX&Y
CX,Y
DX or Y
Attempts:
3 left
💡 Hint
Common Mistakes
Using | which means either type, not both.
Using commas or words instead of &.
3fill in blank
hard

Fix the error in the function parameter type declaration to correctly use intersection types.

PHP
<?php
interface M {}
interface N {}

function example([1] $param) {
    // code
}
?>
Drag options to blanks, or click blank then click option'
AM&N
BM,N
CM|N
DM or N
Attempts:
3 left
💡 Hint
Common Mistakes
Using union type | instead of intersection &.
Separating interfaces with commas.
4fill in blank
hard

Fill both blanks to declare a function that accepts a parameter implementing interfaces P and Q, and returns a value of the same intersection type.

PHP
<?php
interface P {}
interface Q {}

function process([1] $input): [2] {
    return $input;
}
?>
Drag options to blanks, or click blank then click option'
AP&Q
BP|Q
CQ&P
DP,Q
Attempts:
3 left
💡 Hint
Common Mistakes
Using union type | or commas in type declarations.
Mismatching parameter and return types.
5fill in blank
hard

Fill all three blanks to declare a class implementing interfaces U and V, and a function that accepts a parameter with the same intersection type.

PHP
<?php
interface U {}
interface V {}

class MyClass implements [1] {
    public function handle([2] $param): void {
        // handle param
    }
}

function callHandle([3] $obj) {
    $obj->handle($obj);
}
?>
Drag options to blanks, or click blank then click option'
AU&V
BU|V
CV&U
DU,V
Attempts:
3 left
💡 Hint
Common Mistakes
Using & in the implements clause instead of commas.
Using | or commas in intersection type declarations instead of &.