CKTIS / PHP
TypeError
Označi vrednost, ki ne ustreza zahtevanemu tipu argumenta ali rezultata. strict_types prepreči večino skalarnih pretvorb ob klicu uporabniške funkcije. int še vedno ustreza float.
Različica: PHP 8.4; repository runtime, production runtime not attested
Sintaksa
catch (TypeError $failure) { /* handle invalid type */ }Označi vrednost, ki ne ustreza zahtevanemu tipu argumenta ali rezultata. strict_types prepreči večino skalarnih pretvorb ob klicu uporabniške funkcije. int še vedno ustreza float. TypeError deduje od Error in izpolnjuje Throwable.
declare(strict_types=1);
function twice(int $n): int{return $n*2;}
try{twice("2");}catch(TypeError $e){echo json_encode([twice(2),$e instanceof Error,$e instanceof Throwable]);}
[4,true,true]
Primeri
<?php
declare(strict_types=1);
function twice(int $n): int{return $n*2;}
try{twice("2");}catch(TypeError $e){echo json_encode([twice(2),$e instanceof Error,$e instanceof Throwable]);}
Rezultat
[4,true,true]