php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64554 var_export does not export absolute namespace for classname
Submitted: 2013-03-31 05:55 UTC Modified: 2013-10-25 15:22 UTC
From: cornelius dot howl at gmail dot com Assigned: krakjoe (profile)
Status: Closed Package: Documentation problem
PHP Version: 5.4.13 OS: Any
Private report: No CVE-ID: None
 [2013-03-31 05:55 UTC] cornelius dot howl at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/function.var-export#refsect1-function.var-
export-description
---

If I defined a class "Foo\Bar\C" that implements __set_state method, then use 
var_export to export the php code, and put this exported code to another 
namespace, it will causes "class not found".

because the exported class name is "Foo\Bar\C" not "\Foo\Bar\C", the class can not 
be found without the root namespace.  :(

var_export should always export the root namespace.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-03-31 09:48 UTC] laruence@php.net
hmm, I feel this probably make sense, simply add a lead '\\' solve the problem:


diff --git a/ext/standard/var.c b/ext/standard/var.c
index f76a14c..67a0a72 100644
--- a/ext/standard/var.c
+++ b/ext/standard/var.c
@@ -485,6 +485,7 @@ PHPAPI void php_var_export_ex(zval **struc, int level, 
smart_str *buf TSRMLS_DC)
 		}
 		Z_OBJ_HANDLER(**struc, get_class_name)(*struc, &class_name, 
&class_name_len, 0 TSRMLS_CC);

+		smart_str_appendc(buf, '\\');
 		smart_str_appendl(buf, class_name, class_name_len);
 		smart_str_appendl(buf, "::__set_state(array(\n", 21);


but not sure the side-affect, need more testing
 [2013-03-31 12:19 UTC] laruence@php.net
I think similar issue also exists in serialize...
 [2013-03-31 13:37 UTC] laruence@php.net
-Assigned To: +Assigned To: laruence
 [2013-04-15 10:23 UTC] laruence@php.net
change to doc bug, see: http://marc.info/?l=php-internals&m=136599465400822&w=2
 [2013-04-15 10:23 UTC] laruence@php.net
-Assigned To: laruence +Assigned To:
 [2013-04-15 10:24 UTC] laruence@php.net
-Package: *General Issues +Package: Documentation problem
 [2013-04-17 10:50 UTC] hanskrentel at yahoo dot de
Can you add the code where you run into that issue? I could not reproduce this 
with eval(return var_export();) across different namespaces.

http://3v4l.org/3MhAf#v530
 [2013-10-25 15:22 UTC] krakjoe@php.net
Automatic comment from SVN on behalf of krakjoe
Revision: http://svn.php.net/viewvc/?view=revision&revision=331965
Log: #64554 attach warning to var_export and serialize regarding the handling of objects of a namespaced class.
 [2013-10-25 15:22 UTC] krakjoe@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: krakjoe
 [2013-10-25 15:22 UTC] krakjoe@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

I have attached a suitable warning to var_export and serialize function documentation regarding the handling of the leading slash where objects are of a namespaced class.

Thanks for your input.
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Thu Jun 30 20:03:34 2022 UTC