Doc #69811 [Ana]: Cannot bind closure to scope of internal class stdClass
| From: | olivier dot laviale at gmail dot com | Date: | Sun, 14 Jun 2015 21:39:03 +0000 |
| Subject: | Doc #69811 [Ana]: Cannot bind closure to scope of internal class stdClass | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-12428@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=69811&edit=1
ID: 69811
User updated by: olivier dot laviale at gmail dot com
Reported by: olivier dot laviale at gmail dot com
Summary: Cannot bind closure to scope of internal class
stdClass
Status: Analyzed
Type: Documentation Problem
Package: Documentation problem
Operating System: Linux
PHP Version: 7.0.0alpha1
Block user comment: N
Private report: N
New Comment:
Thanks for your reply Kalle. There's indeed an error in my code example, should be as follows:
```
<?php
class A
{
private $a = 1;
protected $b = 2;
public $c = 3;
static public function get_public_vars($object)
{
$get_public_vars = Closure::bind(function($object) {
return get_object_vars($object);
}, null, 'stdClass');
return $get_public_vars($object);
}
}
var_dump(A::get_public_vars(new A));
```
Now that we can't bind
stdClass, what's the best way to get *only* the public
properties of an object, since get_object_vars() executes from the current scope?
Previous Comments:
------------------------------------------------------------------------
[2015-06-13 06:25:20] kalle@php.net
To clarify Laurence's change; This is intended behaviour that you cannot bind a closure to an
class defined internally.
Also I think you have a mistake in the example, as you pass a newly created instance of
'A' to the 'A::get_public_vars()' method, but 'A::get_public_vars()'
still assumes '$this' in the return statement, not that it would render the practice of
binding closures to internally classes anymore allowed ;-)
------------------------------------------------------------------------
[2015-06-12 12:55:13] olivier dot laviale at gmail dot com
Description:
------------
Hi,
Looks like the following code is no longer possible with PHP7.
The following error is raised: "Cannot bind closure to scope of internal class stdClass".
I understand that PHP7 may introduce BC changes, but there's no mention about it in the
documentation: http://php.net/manual/en/closure.bind.php
Test script:
---------------
<?php
class A
{
private $a = 1;
protected $b = 2;
public $c = 3;
static public function get_public_vars()
{
$get_public_vars = Closure::bind(function($object) {
return get_object_vars($object);
}, null, 'stdClass');
return $get_public_vars($this);
}
}
var_dump(A::get_public_vars(new A));
Expected result:
----------------
[ 'c' => 3 ]
Actual result:
--------------
Error: Cannot bind closure to scope of internal class stdClass
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69811&edit=1