I am using type declarations in PHP 8.0.7
with a simple class like:
<?php
class KV {
public function __construct(private array $storage = []) {}
public function put(string $key, string $value): bool {
$this->storage[$key] = $value;
echo "successfully put {$key} => {$value}" . PHP_EOL;
return true;
}
public function getStorage(): array {
return $this->storage;
}
}
$kv = new KV();
$kv->put('foo', 12); // works
$kv->put('bar', 123.2); // works
$kv->put('car', true); // works
print_r($kv->getStorage());
However, to my surprise when I try to invoke $kv->put('foo', 12);
this does not error with invalid argument type. It should, as 12
is not a string right? What am I missing here? There is explicitly a type declaration in PHP for int, float, bool, object, array, and mixed among others.