Complete the code to declare a function that accepts an argument implementing both LoggerInterface and JsonSerializable.
<?php
function logData([1] $data): void {
// function body
}
?>The intersection type LoggerInterface&JsonSerializable ensures the argument implements both interfaces.
Complete the code to declare a class that implements both Countable and IteratorAggregate using intersection types in a method parameter.
<?php class Collection { public function process([1] $item): void { // process item } } ?>
The parameter type Countable&IteratorAggregate requires the argument to implement both interfaces.
Fix the error in the function parameter type declaration to correctly use intersection types.
<?php
function handle([1] $obj): void {
// handle object
}
?>Intersection types use & to require the argument to implement both interfaces.
Fill both blanks to create a function that accepts an argument implementing both ArrayAccess and JsonSerializable, and returns a string.
<?php function exportData([1] $data): [2] { return json_encode($data); } ?>
The parameter uses intersection type ArrayAccess&JsonSerializable and the return type is string.
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 function countItems([1] $collection): [2] { $count = count($collection); return [3]; } ?>
The parameter uses intersection type Countable&IteratorAggregate, the return type is int, and the function returns the variable $count.