CKTIS / PHP
Tipski sistem in deklaracije
Tipske deklaracije preverijo argumente, rezultate in lastnosti.
Različica: PHP 8.4; repository runtime, production runtime not attested
Sintaksa
declare(strict_types=1); function code(int|string $v): string { /* ... */ }Tipske deklaracije preverijo argumente, rezultate in lastnosti. Unija sprejme vsak navedeni tip. strict_types v klicni datoteki prepreči običajne skalarne pretvorbe, int pa še vedno ustreza float. Tabela namesto int ali string sproži 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
Primeri
<?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);}
Rezultat
string int TypeError TypeError float