|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2010-08-30 18:39 UTC] johannes@php.net
-Status: Open
+Status: Bogus
[2010-08-30 18:39 UTC] johannes@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 01:00:02 2025 UTC |
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