Req #52740 [Opn->Bgs]: var_export unnecessarily refers to classes in a namespace sensitive way
| From: | johannes@php.net | Date: | Mon, 30 Aug 2010 16:39:46 +0000 |
| Subject: | Req #52740 [Opn->Bgs]: var_export unnecessarily refers to classes in a namespace sensitive way | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-153940@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=52740&edit=1
ID: 52740
Updated by: johannes@php.net
Reported by: coops at coops dot se
Summary: var_export unnecessarily refers to classes in a
namespace sensitive way
-Status: Open
+Status: Bogus
Type: Feature/Change Request
Package: Class/Object related
Operating System: N/A
PHP Version: 5.3.3
Block user comment: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2010-08-30 16:39:08] 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
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=52740&edit=1