Req #52740 [NEW]: var_export unnecessarily refers to classes in a namespace sensitive way
| From: | coops at coops dot se | Date: | Mon, 30 Aug 2010 14:39:09 +0000 |
| Subject: | Req #52740 [NEW]: var_export unnecessarily refers to classes in a namespace sensitive way | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-153927@lists.php.net to get a copy of this message | ||
From:
Operating system: N/A
PHP version: 5.3.3
Package: Class/Object related
Bug Type: Feature/Change Request
Bug description:var_export unnecessarily refers to classes in a namespace sensitive way
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 bug report at http://bugs.php.net/bug.php?id=52740&edit=1
--
Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=52740&r=trysnapshot52
Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=52740&r=trysnapshot53
Try a snapshot (trunk): http://bugs.php.net/fix.php?id=52740&r=trysnapshottrunk
Fixed in SVN: http://bugs.php.net/fix.php?id=52740&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=52740&r=needdocs
Fixed in release: http://bugs.php.net/fix.php?id=52740&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=52740&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=52740&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=52740&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=52740&r=support
Expected behavior: http://bugs.php.net/fix.php?id=52740&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=52740&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=52740&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=52740&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=52740&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=52740&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=52740&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=52740&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=52740&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=52740&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=52740&r=mysqlcfg