ID:               25837
 User updated by:  manu at chasqui dot cu
 Reported By:      manu at chasqui dot cu
 Status:           Wont fix
 Bug Type:         Variables related
-Operating System: Windows Xp-Pro/Apache 1.3.24
+Operating System: Windows Xp-Pro/Apache 1.3.31
-PHP Version:      4.3.3
+PHP Version:      4.3.8
 New Comment:

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:
<?php

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;            
    }
    else
        //  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);
?>


Previous Comments:
------------------------------------------------------------------------

[2004-01-21 15:00:57] 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.

Regards,
Manu.

------------------------------------------------------------------------

[2004-01-17 13:19:21] [EMAIL PROTECTED]

Nah, it's not a bug. Just don't do it

------------------------------------------------------------------------

[2004-01-17 10:06:29] [EMAIL PROTECTED]

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.

------------------------------------------------------------------------

[2003-10-11 10:26:29] manu at chasqui dot cu

Description:
------------
var_export fails to reproduce valid code when dumping several nested
objects.

Reproduce code:
---------------
<?php
class VE_Test
{
    var $child;
}

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

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

var_export($ve_parent);
?>

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


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=25837&edit=1

Reply via email to