Come posso prendere un “errore fatale catchable” sul tipo di PHP hinting?
-
20-09-2019 - |
Domanda
Sto cercando di implementare Tipo Hinting di PHP5 su uno della mia classe,
class ClassA {
public function method_a (ClassB $b)
{}
}
class ClassB {}
class ClassWrong{}
Utilizzo corretto:
$a = new ClassA;
$a->method_a(new ClassB);
la produzione di errore:
$a = new ClassA;
$a->method_a(new ClassWrong);
errore fatale catchable: Argomento 1 passata a ClasseA :: method_a () deve essere un'istanza di ClassB, istanza di ClassWrong dato ...
Si può sapere se è possibile raggiungere tale errore (poiché dice "catturabile")? e se sì, come?
Grazie.
Soluzione
Aggiornamento: Questo non è un errore fatale catturabile più in php 7. Invece una "eccezione" è gettata. Una "eccezione" (in virgolette) che non è derivato da Eccezione ma Errore ; è ancora un Throwable e può essere gestito con un normale blocco try-catch. vedi https://wiki.php.net/rfc/throwable-interface
per es.
<?php
class ClassA {
public function method_a (ClassB $b) { echo 'method_a: ', get_class($b), PHP_EOL; }
}
class ClassWrong{}
class ClassB{}
class ClassC extends ClassB {}
foreach( array('ClassA', 'ClassWrong', 'ClassB', 'ClassC') as $cn ) {
try{
$a = new ClassA;
$a->method_a(new $cn);
}
catch(Error $err) {
echo "catched: ", $err->getMessage(), PHP_EOL;
}
}
echo 'done.';
stampe
catched: Argument 1 passed to ClassA::method_a() must be an instance of ClassB, instance of ClassA given, called in [...]
catched: Argument 1 passed to ClassA::method_a() must be an instance of ClassB, instance of ClassWrong given, called in [...]
method_a: ClassB
method_a: ClassC
done.
Old risposta per le versioni pre-php7:
http://docs.php.net/errorfunc.constants dice:
E_RECOVERABLE_ERROR (intero)
errore fatale catchable. Esso indica che un errore di probabilmente pericoloso si è verificato, ma non ha lasciato il motore in uno stato instabile. Se l'errore non è catturato da una maniglia definito dall'utente (vedi anche set_error_handler () ), l'applicazione interrompe come è stato un E_ERROR.
vedi anche: http://derickrethans.nl/erecoverableerror.html
per es.
function myErrorHandler($errno, $errstr, $errfile, $errline) {
if ( E_RECOVERABLE_ERROR===$errno ) {
echo "'catched' catchable fatal error\n";
return true;
}
return false;
}
set_error_handler('myErrorHandler');
class ClassA {
public function method_a (ClassB $b) {}
}
class ClassWrong{}
$a = new ClassA;
$a->method_a(new ClassWrong);
echo 'done.';
stampe
'catched' catchable fatal error
done.
modifica: Ma si può "fare" è un'eccezione è possibile gestire con un blocco try-catch
function myErrorHandler($errno, $errstr, $errfile, $errline) {
if ( E_RECOVERABLE_ERROR===$errno ) {
echo "'catched' catchable fatal error\n";
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
// return true;
}
return false;
}
set_error_handler('myErrorHandler');
class ClassA {
public function method_a (ClassB $b) {}
}
class ClassWrong{}
try{
$a = new ClassA;
$a->method_a(new ClassWrong);
}
catch(Exception $ex) {
echo "catched\n";
}
echo 'done.';