php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #63021 var_export() does not export Closures
Submitted: 2012-09-06 10:36 UTC Modified: 2012-09-06 11:45 UTC
Votes:10
Avg. Score:4.6 ± 0.7
Reproduced:10 of 10 (100.0%)
Same Version:2 (20.0%)
Same OS:1 (10.0%)
From: dexen dot devries at gmail dot com Assigned:
Status: Open Package: *Programming Data Structures
PHP Version: 5.4.6 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: dexen dot devries at gmail dot com
New email:
PHP Version: OS:

 

 [2012-09-06 10:36 UTC] 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(
))

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-09-06 11:45 UTC] laruence@php.net
-Type: Bug +Type: Feature/Change Request
 [2012-09-06 11:45 UTC] laruence@php.net
closure can not be serialized.. change to feature request instead.
 [2014-03-17 15:15 UTC] 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.
 [2019-04-09 10:16 UTC] benjamin dot morel at gmail dot com
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!
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat Aug 17 23:01:27 2019 UTC