php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #52740 var_export unnecessarily refers to classes in a namespace sensitive way
Submitted: 2010-08-30 16:39 UTC Modified: 2010-08-30 18:39 UTC
From: coops at coops dot se Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.3.3 OS: N/A
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: coops at coops dot se
New email:
PHP Version: OS:

 

 [2010-08-30 16:39 UTC] coops at coops dot se
Description:
------------
I use var export to generate PHP code fragments as one of it's strength is that it's output is an expression of parseable PHP. However, when var_export refers to classes, it refers to their relative name (from root) instead of their absolute name, making you unable to use the output in namespace context (contexts with a specified namespace).

Too illustrate this problem, say we're exporting an instance of the class 'foo\bar\BazClass'. var_export will now return something like:

"foo\bar\BazClass::__set_state(...)"

If inserted into a file with a beginning declaration "namespace foo_space" this code will not work as the class in the expression would refer to foo_space\foo\bar\BazClass which is incorrect and probably throws a fatal error.

A simple fix is to make the ouput of var_export return namespaced classes with an initial '\' e.g. instead of the above, make it return:

"\foo\bar\BazClass::__set_state(...)"

This would make the expression work in any namespace context.

Test script:
---------------
<?php namespace foo\bar;

class BazClass {
    public $x = 3.14;
    public $y = 5.14;
}

$baz = new BazClass();
$baz->x = 4.14;
$baz->y = 5.14;

$file = tempnam(sys_get_temp_dir(), "tst");
file_put_contents($file, '<?php namespace foo_space; return ' . var_export($baz, true) . ";");

$class = require($file);
echo $class->x + $class->y;



Expected result:
----------------
9.28

Actual result:
--------------
Fatal error: Class 'foo_space\foo\bar\BazClass' not found in C:\Windows\Temp\tst88FE.tmp on line 1

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-08-30 18:39 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2010-08-30 18:39 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

We would have to add the \ also for non-namespaced classes. This would break compatibility for people exchanging exported data with 5.2 or some other cases.

Don't define a namespace inside the var_export-file and you are fine.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue May 13 13:01:27 2025 UTC