101

I am trying to implement Type Hinting of PHP5 on one of my class,

class ClassA {
    public function method_a (ClassB $b)
    {}
}

class ClassB {}
class ClassWrong{}

Correct usage:

$a = new ClassA;
$a->method_a(new ClassB);

producing error:

$a = new ClassA;
$a->method_a(new ClassWrong);

Catchable fatal error: Argument 1 passed to ClassA::method_a() must be an instance of ClassB, instance of ClassWrong given...

Is it possible to catch that error(since it says "catchable")? and if yes, how?

1
  • 4
    For future reference: Exceptions in the engine (for PHP 7) - Beginning with PHP 7 it is possible catch fatal errors. This is also for the here discussed “Catchable fatal error” (E_RECOVERABLE_ERROR) as these be catched beginning with PHP 7..
    – hakre
    Commented Apr 27, 2015 at 6:11

1 Answer 1

117

Update: This is not a catchable fatal error anymore in php 7. Instead an "exception" is thrown. An "exception" (in scare quotes) that is not derived from Exception but Error; it's still a Throwable and can be handled with a normal try-catch block. see https://wiki.php.net/rfc/throwable-interface

E.g.

<?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.';

prints

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 answer for pre-php7 versions:
http://docs.php.net/errorfunc.constants says:

E_RECOVERABLE_ERROR ( integer )
Catchable fatal error. It indicates that a probably dangerous error occured, but did not leave the Engine in an unstable state. If the error is not caught by a user defined handle (see also set_error_handler()), the application aborts as it was an E_ERROR.

see also: http://derickrethans.nl/erecoverableerror.html

e.g.

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.';

prints

'catched' catchable fatal error
done.

edit: But you can "make" it an exception you can handle with a try-catch block

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.';

see: http://docs.php.net/ErrorException

8
  • 2
    So of course this behaves very much like a fatal error, except when you look in your server logs you won't find it. Thanks php :/
    – John Hunt
    Commented Feb 10, 2016 at 8:46
  • 5
    so in other words you can't catch a catchable error. Wonderful! Commented Mar 17, 2016 at 17:45
  • @Paul what brings you to this conclusion?
    – VolkerK
    Commented Mar 18, 2016 at 8:36
  • 3
    Oh, I just meant it wasn't catchable in the traditional sense (using a try/catch block). I was just feeling grumpy about PHP that day, so when I found out that it was 'catchable' in a totally different sense, I felt compelled to comment. Nothing against your wonderful answer (which in fact I upvoted); all my ire was for PHP itself! Commented Mar 24, 2016 at 17:05
  • 1
    I have PHP 7.0.12 and cannot catch Catchable fatal error: Object of class stdClass could not be converted to string with any of TypeError, Error, Exception or \Throwable - any ideas?
    – user9645
    Commented Dec 7, 2016 at 17:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.