|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #25837 var-exports fails to produce valid PHP in some cases
Submitted: 2003-10-11 10:26 UTC Modified: 2004-08-15 15:28 UTC
From: manu at chasqui dot cu Assigned:
Status: Wont fix Package: Variables related
PHP Version: 4.3.8 OS: Windows Xp-Pro/Apache 1.3.31
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2003-10-11 10:26 UTC] manu at chasqui dot cu
var_export fails to reproduce valid code when dumping several nested objects.

Reproduce code:
class VE_Test
    var $child;

$ve_child = new VE_Test();
$ve_child->child = NULL;

$ve_parent = new VE_Test();
$ve_parent->child = &$ve_child;


Expected result:
Actually I can't make a proposal without requiring more from PHP:

with (new VE_Test()){
  child = 
    with (new VE_Test()){
       child = NULL;

This is just a proposal, there can be other better ways.

Actual result:
class ve_test {
  var $child =
  class ve_test {
    var $child = NULL;

Which fails to compile:
Parse error: parse error, unexpected T_CLASS in d:\inetpub\tests\tests\php\vd.php on line 4


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2004-01-17 10:06 UTC]
Well, var_export() is almost meaningless for objects since it gives you the internal representation by "class fubar ..." however in a runnning system a class can be defined only once - so the usage of var_export() with objects is quite limited. Additionally circular references are hardly handled by most of the PHP functions and even by the core (see PHP5 which currently does not destruct objects if there is a circular reference).
However moving that to bug, since var_export() should fail if some of the member variables is an object itself.
 [2004-01-17 13:19 UTC]
Nah, it's not a bug. Just don't do it
 [2004-01-21 15:00 UTC] manu at chasqui dot cu
I don't think it is a bug, neither I think it could be hard to implement. I can't make a test right now, because I?m not connected using my PC; but I think that such a behavior could be easily simulated using the get_class_vars and get_parent_class functions - but it'd be less eficient than if done in the var_export function (C code and compiled).

I promise once I get home I will write a php function to simulated the expected behaviour.

Notice I don't expect to get the class definition, but that every class have been already defined.

 [2004-08-15 15:28 UTC] manu at chasqui dot cu
As I promised *long ago*, here is a small function that implements the idea. Notice, I left circular reference loops unchecked, though it is not hard to add this check. I did this in part, because any way I wasn't changing the very var_export function :)
Again, notice I do not expect the class definition.

Test code:

function obj_var_export($obj)
    //  TODO:   Maintain a global list of references and fix-ups
    //      to avoid forever-loops when circular references 
    //      are found.
    if (is_object($obj))
        $classn = get_class($obj);
        $result = "with (new $classn())\n{\n";
        $vars = get_object_vars($obj);
        foreach($vars as $n => $v)
            $result .= "\$$n = ".obj_var_export($v)."\n";
        $result .= "}\n";
        return $result;            
        //  var_export would be modified in order that any component
        //  of obj is an object (e.g an array containing objects)
        return var_export($obj, true).";";  

class VE_Test
    var $child;
    var $str = "hi!";

$ve_child = new VE_Test();
$ve_child->child = NULL;

$ve_parent = new VE_Test();
$ve_parent->child = &$ve_child;

echo obj_var_export($ve_parent);
echo "<br/><br/>";
echo var_export($ve_parent);
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Apr 15 00:01:28 2024 UTC