php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #31578 Type hinting fata error
Submitted: 2005-01-17 02:25 UTC Modified: 2005-01-17 08:11 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: mgkimsal at conduit dot com Assigned:
Status: Wont fix Package: Feature/Change Request
PHP Version: 5.0.2 OS: all
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mgkimsal at conduit dot com
New email:
PHP Version: OS:

 

 [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 
 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-01-17 08:11 UTC] derick@php.net
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.
 [2012-07-16 13:45 UTC] notdefix at hotmail dot com
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');
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jul 01 10:01:29 2024 UTC