php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69909 Type hint of a function are not checked when it's a generator
Submitted: 2015-06-23 09:37 UTC Modified: 2018-01-10 19:09 UTC
Votes:2
Avg. Score:2.5 ± 0.5
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: lyrixx at lyrixx dot info Assigned: nikic (profile)
Status: Closed Package: *General Issues
PHP Version: 5.5.26 OS: linux
Private report: No CVE-ID: None
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
35 - 25 = ?
Subscribe to this entry?

 
 [2015-06-23 09:37 UTC] lyrixx at lyrixx dot info
Description:
------------
If you don't "next" on the generator, the typehint of the function is not checked.

Test script:
---------------
<?php

function hello(closure $closure)
{
    $closure('hello');
    yield 10;
}

hello();


Expected result:
----------------
Argument 1 passed to hello must be an instance of Closure, none given


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-06-23 12:01 UTC] jpauli@php.net
This is because the call to the generator does not call the function actually, thus this latter can't check its arguments.

However, triggering a method on the generator returns actually triggers the function call.

I attached a patch that fixes the issue by checking args in case of generator creation, however, this will only work if you make use of the return value (which is not the case in your script)

Patch is at https://github.com/jpauli/php-src/tree/fix-69909
 [2018-01-10 19:09 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2018-01-10 19:09 UTC] nikic@php.net
Fixed in 7.1 The argument binding code is now executed before the generator is suspended.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 14 20:01:55 2025 UTC