|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2005-01-17 02:25 UTC] mgkimsal at conduit dot com
Description: ------------ http://us3.php.net/manual/en/language.oop5.typehinting.php states that "Failing to satisfy the type hint results in a fatal error." This seems relatively useless compared to having PHP5 throw an exception. This would make it a recoverable situation that could be handled gracefully rather than yet another fatal error which PHP can't deal with. http://bugs.php.net/bug.php?id=28001&edit=2 had a response from Derick stating that someone should just write their own custom error handler to catch this and deal with it, but it's a fatal error - my php5.0.2 can not catch fatal errors, and I'm not seeing anything in the docs that says we should be able to handle fatal errors with user-defined code. I *DID* see an example file that caught a FATAL error *when it was invoked by trigger_error()* but it doesn't work in the code below. Reproduce code: --------------- <?php set_error_handler("myfunc"); error_reporting(E_ALL); function myfunc($errno, $errstr, $errfile, $errline) { echo "err=$errono"; print_r(debug_backtrace()); } class bar { } class foo { function me(bar $b) { print_r($b); } } $b = new bar; $f = new foo(); $f->me("fff"); ?> Expected result: ---------------- I would expect a backtrace() dump on the screen. Actual result: -------------- PHP Fatal error: Argument 1 must be an object of class bar in /var/www/html/v5/er.php on line 12 Fatal error: Argument 1 must be an object of class bar in /var/www/html/v5/er.php on line 12 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 24 02:00:01 2025 UTC |
It is possible to do your own error catching and allow them to be non-fatel; however, the performance impact is (unacceptably) huge! <?php function typeHinting($errorCode, $errorMessage) { if ($errorCode == E_RECOVERABLE_ERROR && preg_match('/^Argument \d+ passed to (?:\w+::)?\w+\(\) must be an instance of (?:integer|int|float|double|string|boolean|bool), (?:integer|float|double|string|boolean) given/', $errorMessage)) { // Traditional type hinting with scalar values (int, float, bool or string) is not supported by PHP. This workaround suppresses errors when the names of both expected and given type are scalar values // Although string based equallity checking is a poor substitute for actual type comparison, it will work in almost all cases. return true; } return false; // allow next error_handler to pick up } set_error_handler('typeHinting');