Edit report at https://bugs.php.net/bug.php?id=31578&edit=1
ID: 31578
Comment by: notdefix at hotmail dot com
Reported by: mgkimsal at conduit dot com
Summary: Type hinting fata error
Status: Wont fix
Type: Feature/Change Request
Package: Feature/Change Request
Operating System: all
PHP Version: 5.0.2
Block user comment: N
Private report: N
New Comment:
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');
Previous Comments:
------------------------------------------------------------------------
[2005-01-17 08:11:12] [email protected]
We decided in the past that this should be a fatal error, and not catchable. If
you want to make sure your objects are the correct class, you have to test that
before you call the method with the type hint.
------------------------------------------------------------------------
[2005-01-17 02:25:17] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=31578&edit=1