php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #74277 Closure::bind does not require new scope to be string
Submitted: 2017-03-20 13:36 UTC Modified: 2017-03-20 13:51 UTC
From: bburnichon at gmail dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 7.0.17 OS: Linux Ubuntu 16.04
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: bburnichon at gmail dot com
New email:
PHP Version: OS:

 

 [2017-03-20 13:36 UTC] bburnichon at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/closure.bind
---

On Manual page, it is mentioned that new scope should not be an instance but just class name. I checked behavior with https://3v4l.org/rrY4u and no notice were seen nor Closure::bindTo returns false.

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

class Foo { private $foo = 'foo'; }

$getState = function () { return $this->foo; };
$foo = new Foo();

$getStateBound = $getState->bindTo($foo, $foo);
var_dump($getStateBound());

$viaStatic = Closure::bind($getState, $foo, $foo);
var_dump($viaStatic());

Expected result:
----------------
I expect at least 2 notices for last argument of bindTo and bind not being strings.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-03-20 13:49 UTC] daverandom@php.net
The docs don't specifically say that the new scope must be a string, they only indicate that the default value is equivalent to the string "static".

In fact the scope can be a string containing a class name, or an object (or null).

https://lxr.room11.org/xref/php-src%40master/Zend/zend_closures.c#196-210

The docs should maybe be a little more explicit about what is permitted.
 [2017-03-20 13:51 UTC] daverandom@php.net
-Status: Open +Status: Not a bug
 [2017-03-20 13:51 UTC] daverandom@php.net
On re-reading, I notice that the description of this argument does in fact state than an object can be used.
 [2017-03-20 14:08 UTC] bburnichon at gmail dot com
I'm OK with the description, but in the changelog part http://php.net/manual/en/closure.bind.php#refsect1-closure.bind-changelog

It is stated that starting from PHP 7.0.0, newscope can not be (an object of) an internal class.

I think that what was misleading me, not all objects are rejected, only stdClass instances. Anyway, I think then that this notice should be added to Closure::bindTo documentation.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 13:01:33 2025 UTC