0
0
PHPprogramming~10 mins

Intersection types in practice 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 an argument implementing both LoggerInterface and JsonSerializable.

PHP
<?php
function logData([1] $data): void {
    // function body
}
?>
Drag options to blanks, or click blank then click option'
AJsonSerializable
BLoggerInterface|JsonSerializable
CLoggerInterface&JsonSerializable
DLoggerInterface
Attempts:
3 left
💡 Hint
Common Mistakes
Using | instead of & for intersection types
Specifying only one interface
2fill in blank
medium

Complete the code to declare a class that implements both Countable and IteratorAggregate using intersection types in a method parameter.

PHP
<?php
class Collection {
    public function process([1] $item): void {
        // process item
    }
}
?>
Drag options to blanks, or click blank then click option'
ACountable&IteratorAggregate
BCountable
CCountable|IteratorAggregate
DIteratorAggregate
Attempts:
3 left
💡 Hint
Common Mistakes
Using | instead of &
Using only one interface
3fill in blank
hard

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

PHP
<?php
function handle([1] $obj): void {
    // handle object
}
?>
Drag options to blanks, or click blank then click option'
ATraversable&Countable
BTraversable|Countable
CTraversable
DCountable
Attempts:
3 left
💡 Hint
Common Mistakes
Using | instead of &
Using only one interface
4fill in blank
hard

Fill both blanks to create a function that accepts an argument implementing both ArrayAccess and JsonSerializable, and returns a string.

PHP
<?php
function exportData([1] $data): [2] {
    return json_encode($data);
}
?>
Drag options to blanks, or click blank then click option'
AArrayAccess&JsonSerializable
Bstring
Cint
DArrayAccess|JsonSerializable
Attempts:
3 left
💡 Hint
Common Mistakes
Using | instead of &
Wrong return type
5fill in blank
hard

Fill all three blanks to define a function that accepts an argument implementing both Countable and IteratorAggregate, returns an int, and uses a variable to store the count.

PHP
<?php
function countItems([1] $collection): [2] {
    $count = count($collection);
    return [3];
}
?>
Drag options to blanks, or click blank then click option'
ACountable&IteratorAggregate
Bint
C$count
DCountable|IteratorAggregate
Attempts:
3 left
💡 Hint
Common Mistakes
Using | instead of &
Returning wrong variable or type