php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #42520 Type hinting: function still gets called if type mismatch occurs
Submitted: 2007-09-02 14:24 UTC Modified: 2007-11-21 17:09 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: konstantin at boyandin dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 5.2.4 OS: Windows XP
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: konstantin at boyandin dot com
New email:
PHP Version: OS:

 

 [2007-09-02 14:24 UTC] konstantin at boyandin dot com
Description:
------------
Function with type hinting is executed nonetheless, even if type mismatch exception occurs.

Reproduce code:
---------------
<?php
$a = new B();
test($a);

class A {
    public $name = 'A';
       
    public function show() {
        print($this->name);
    }
}

class B {
    public $name = 'B';
       
    public function show() {
        print($this->name);
    }
}

function test(A $obj) {
        $obj->show();
}
?>

Expected result:
----------------
Fatal error, nothing displayed 9save error info if output to the browsers)

Actual result:
--------------
Function is called as if required type was correct *and* fatal error is generated.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-09-03 08:03 UTC] rquadling@php.net
I'm only seeing the error, no output using PHP 5.2.4RC4-dev.

What version are you using that shows the 'A' or 'B' as well as the error message?
 [2007-09-03 08:14 UTC] bjori@php.net
his example is missing the custom error handler catching the E_RECOVERABLE_ERROR.
 [2007-09-03 08:20 UTC] bjori@php.net
bjori@lindsay:~$ php
<?php
function err($errno, $errstr) {
 echo $errstr, "\n";
}
set_error_handler("err");
function typeHint(array $foo) {
 echo "I am inside the type hint with :", gettype($foo), "\n";
}

typeHint("string");

Argument 1 passed to typeHint() must be an array, string given, called in /home/bjori/- on line 10 and defined
I am inside the type hint with :string

 [2007-09-03 09:09 UTC] konstantin at boyandin dot com
I was using the Windows binary PHP 5.2.4 distribution.

Note: xdebug was installed when I found that behavior (no explicit options for it set in php.ini). I will try the situation without xdebug but with error handler set/unset.
 [2007-09-05 09:57 UTC] konstantin at boyandin dot com
I have revised the reproducible code in accordance with the advice given. Now it demonstrates that the function is called in pure PHP 5.2.4 installation.

<?php
set_error_handler("report_error");
$a = new B();
test($a);

class A {
    public function show() {
	print('Class A used');
    }
}

class B {
    public function show() {
	print('Class B used');
    }
}

function test(A $obj) {
    $obj->show();
}

function report_error($errno, $errstr) {
    echo "PHP error $errstr<br />";
}
?>
 [2007-09-06 07:50 UTC] rquadling@php.net
The code is only executed if you are catching recoverable errors (errno = 4096 = E_RECOVERABLE_ERROR).

This is documented as ...

"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."

This is available since PHP V5.2.0

http://www.php.net/manual/en/ref.errorfunc.php#errorfunc.constants.errorlevels.e-recoverable-error

So, this IS documented.

If you return False from set_error_handler the code aborts at that stage with the Catchable fatal error report.



 [2007-09-06 07:53 UTC] rquadling@php.net
The returning False for the set_error_handler function is documented at http://www.php.net/manual/en/function.set-error-handler.php and is available since V5.2.0


 [2007-11-21 17:09 UTC] vrana@php.net
Nothing to document.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 02 18:01:34 2025 UTC