Req #63021 [Com]: var_export() does not export Closures
| From: | benjamin dot morel at gmail dot com | Date: | Tue, 09 Apr 2019 10:16:17 +0000 |
| Subject: | Req #63021 [Com]: var_export() does not export Closures | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-220377@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=63021&edit=1
ID: 63021
Comment by: benjamin dot morel at gmail dot com
Reported by: dexen dot devries at gmail dot com
Summary: var_export() does not export Closures
Status: Open
Type: Feature/Change Request
Package: *Programming Data Structures
PHP Version: 5.4.6
Block user comment: N
Private report: N
New Comment:
For anyone interested, I just released a userland implementation of var_export() that supports
exporting closures:
https://github.com/brick/varexporter
It's based on nikic's PHP-Parser library, and works pretty well, with a few caveats!
Previous Comments:
------------------------------------------------------------------------
[2014-03-17 15:15:55] benjamin dot morel at strictcoding dot co dot uk
This would be great. Could the parameter list and PHP code defined in the closure be passed to
__set_state()? Something like:
function ($a, $b) { return 'hi'; }
__set_state(array(
'parameters' => array(
'a' => array( ... )
'b' => array( ... )
),
'code' => 'return \'hi\';'
));
It might be tricky to export the use()d variables, though.
------------------------------------------------------------------------
[2012-09-06 11:45:05] laruence@php.net
closure can not be serialized.. change to feature request instead.
------------------------------------------------------------------------
[2012-09-06 10:36:21] dexen dot devries at gmail dot com
Description:
------------
While var_dump() produces sensible output for Closures, output of the var_export() is pretty much
useless.
Test script:
---------------
<?php
$a = range(1, 3);
$foo = function($n) use($a) {
return $a[$n];
};
var_dump($foo);
var_export($foo);
Expected result:
----------------
object(Closure)#1 (2) {
["static"]=>
array(1) {
["a"]=>
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
}
["parameter"]=>
array(1) {
["$n"]=>
string(10) "<required>"
}
}
(((resembling the following:)))
object(Closure)#1 (2) {
["static"]=>
array(1) {
["a"]=>
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
}
["parameter"]=>
array(1) {
["$n"]=>
string(10) "<required>"
}
}
Actual result:
--------------
object(Closure)#1 (2) {
["static"]=>
array(1) {
["a"]=>
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
}
["parameter"]=>
array(1) {
["$n"]=>
string(10) "<required>"
}
}
Closure::__set_state(array(
))
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=63021&edit=1