<?php
declare(strict_types=1);
/**
* Write a function that accepts an array of strings and return another array
* with strings in the input array set as keys.
*
* @param string[] $arrOfStrings An array of strings.
* @return array Another array with strings in the input array set as keys.
*/
function foo(array $arrOfStrings): array
{
return array_fill_keys(array_filter($arrOfStrings, 'is_string'), null);
}
/**
* Now write another function that accepts the output of the previous function,
* as first argument and another string as second argument. In this function,
* assume that you have some reason to iterate over the the input array using
* foreach, key => value format. In every iteration, in addition to other stuff,
* you also have to check if the key is equal to the input string (the second
* parameter of this function), and if they match do some stuff....
*
* @param array $outputFromFoo The output from {@link foo()}.
*/
function bar(array $outputFromFoo, string $str)
{
foreach ($outputFromFoo as $key => $value) {
// other stuff
if ($key === $str) {
echo "Found $str!";
}
}
}
$result = foo(array('ab', 'cd', '10'));
bar($result, 10);
Fatal error: Uncaught TypeError: bar(): Argument #2 ($str) must be of type string, int given, called in /in/DovnC on line 38 and defined in /in/DovnC:26
Stack trace:
#0 /in/DovnC(38): bar(Array, 10)
#1 {main}
thrown in /in/DovnC on line 26
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
Fatal error: Uncaught TypeError: bar(): Argument #2 ($str) must be of type string, int given, called in /in/DovnC on line 38 and defined in /in/DovnC:26
Stack trace:
#0 /in/DovnC(38): bar(Array, 10)
#1 {main}
thrown in /in/DovnC on line 26
Process exited with code 255.
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
Fatal error: Uncaught TypeError: Argument 2 passed to bar() must be of the type string, int given, called in /in/DovnC on line 38 and defined in /in/DovnC:26
Stack trace:
#0 /in/DovnC(38): bar(Array, 10)
#1 {main}
thrown in /in/DovnC on line 26
Process exited with code 255.
Fatal error: Uncaught TypeError: Argument 2 passed to bar() must be of the type string, integer given, called in /in/DovnC on line 38 and defined in /in/DovnC:26
Stack trace:
#0 /in/DovnC(38): bar(Array, 10)
#1 {main}
thrown in /in/DovnC on line 26
Process exited with code 255.
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.28
Warning: Unsupported declare 'strict_types' in /in/DovnC on line 2
Parse error: syntax error, unexpected ':', expecting '{' in /in/DovnC on line 11
Process exited with code 255.