CKTIS / PHP
Sistema di tipi e dichiarazioni
Le dichiarazioni controllano argomenti, risultati e proprietà.
Versione: PHP 8.4; repository runtime, production runtime not attested
Sintassi
declare(strict_types=1); function code(int|string $v): string { /* ... */ }Le dichiarazioni controllano argomenti, risultati e proprietà. Un’unione accetta ogni tipo elencato. strict_types del file chiamante impedisce le normali conversioni scalari, ma int resta ammesso per float. Un array al posto di int o string causa TypeError.
declare(strict_types=1); function code(int|string $v):string{return get_debug_type($v);}
echo code("7")," ",code(7);try{code([]);}catch(TypeError $e){echo " ",get_class($e);}
function countOnly(int $v):int{return $v;} function decimal(float $v):string{return get_debug_type($v);} try{countOnly("7");}catch(TypeError $e){echo "\n",get_class($e)," ",decimal(7);}
string int TypeError
TypeError float
Esempi
<?php
declare(strict_types=1); function code(int|string $v):string{return get_debug_type($v);}
echo code("7")," ",code(7);try{code([]);}catch(TypeError $e){echo " ",get_class($e);}
function countOnly(int $v):int{return $v;} function decimal(float $v):string{return get_debug_type($v);} try{countOnly("7");}catch(TypeError $e){echo "\n",get_class($e)," ",decimal(7);}
Risultato
string int TypeError TypeError float