Skip to content

Commit

Permalink
Fix php#8232 - always reference classes in var_export() via their FQCN
Browse files Browse the repository at this point in the history
This fix corrects a behavior of `var_export()` that was mostly "hidden" until PHP 8.1 introduced:

 * properties with object initializers
 * constants containing object references
 * default values of class properties containing `enum`s

Since `var_export(..., true)` is mostly used in conjunction with code generation,
and we cannot make assumptions about the generated code being placed in the root
namespace, we must always provide the FQCN of a class in exported code.

For example:

```php
<?php

namespace MyNamespace { class Foo {} }

namespace { echo "<?php\n\nnamespace Example;\n\n" . var_export(new \MyNamespace\Foo(), true) . ';'; }
```

produces:

```php
<?php

namespace Example;

MyNamespace\Foo::__set_state(array(
));
```

This code snippet is invalid, because `Example\MyNamespace\Foo::__set_state()` (which
does not exist) is called.

With this patch applied, the code looks like following (valid):

```php
<?php

namespace Example;

\MyNamespace\Foo::__set_state(array(
));
```

Ref: php#8232
Ref: Ocramius/ProxyManager#754
  • Loading branch information
Ocramius committed Mar 21, 2022
1 parent 21b0da8 commit a53ad90
Show file tree
Hide file tree
Showing 19 changed files with 678 additions and 649 deletions.
4 changes: 2 additions & 2 deletions Zend/tests/bug73350.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ $e = new Exception();
var_export($e);
?>
--EXPECTF--
Exception::__set_state(array(
\Exception::__set_state(array(
'message' => '',
'string' => 'Exception in %sbug73350.php:%d
Stack trace:
#0 {main}',
'code' => 0,
'file' => '%sbug73350.php',
'line' => %d,
'trace' =>
'trace' =>
array (
),
'previous' => NULL,
Expand Down
28 changes: 28 additions & 0 deletions Zend/tests/enum/enum-in-var-export.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
Enum in var_export()
--FILE--
<?php

namespace {
enum Foo { case BAR; }
}

namespace A {
enum Foo { case BAR; }
}

namespace A\B {
enum Foo { case BAR; }
}

namespace Test {
var_export(\Foo::BAR);
var_export(\A\Foo::BAR);
var_export(\A\B\Foo::BAR);
}

?>
--EXPECT--
\Foo::BAR
\A\Foo::BAR
\A\B\Foo::BAR
4 changes: 2 additions & 2 deletions ext/date/tests/bug52113.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ $diff_un = unserialize($diff_s);
$p = new DatePeriod($start, $diff_un, 2);
var_dump($diff_un, $p);

$unser = DateInterval::__set_state(array(
$unser = \DateInterval::__set_state(array(
'y' => 7,
'm' => 6,
'd' => 5,
Expand Down Expand Up @@ -68,7 +68,7 @@ object(DateInterval)#%d (16) {
int(0)
}
string(332) "O:12:"DateInterval":16:{s:1:"y";i:0;s:1:"m";i:0;s:1:"d";i:0;s:1:"h";i:4;s:1:"i";i:0;s:1:"s";i:0;s:1:"f";d:0;s:7:"weekday";i:0;s:16:"weekday_behavior";i:0;s:17:"first_last_day_of";i:0;s:6:"invert";i:0;s:4:"days";i:0;s:12:"special_type";i:0;s:14:"special_amount";i:0;s:21:"have_weekday_relative";i:0;s:21:"have_special_relative";i:0;}"
DateInterval::__set_state(array(
\DateInterval::__set_state(array(
'y' => 0,
'm' => 0,
'd' => 0,
Expand Down
136 changes: 68 additions & 68 deletions ext/intl/tests/dateformat_format.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -157,240 +157,240 @@ ut_run();
Input timestamp is : 0
------------

IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
Formatted timestamp is : Wednesday, December 31, 1969 2:00:00 PM GMT-10:00
IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
Formatted timestamp is : December 31, 1969 2:00:00 PM GMT-10:00
IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
Formatted timestamp is : Dec 31, 1969 2:00:00 PM
IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
Formatted timestamp is : 12/31/69 2:00 PM
IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
Formatted timestamp is : 19691231 02:00 PM
------------

Input timestamp is : -1200000
------------

IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
Formatted timestamp is : Wednesday, December 17, 1969 4:40:00 PM GMT-10:00
IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
Formatted timestamp is : December 17, 1969 4:40:00 PM GMT-10:00
IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
Formatted timestamp is : Dec 17, 1969 4:40:00 PM
IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
Formatted timestamp is : 12/17/69 4:40 PM
IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
Formatted timestamp is : 19691217 04:40 PM
------------

Input timestamp is : 1200000
------------

IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
Formatted timestamp is : Wednesday, January 14, 1970 11:20:00 AM GMT-10:00
IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
Formatted timestamp is : January 14, 1970 11:20:00 AM GMT-10:00
IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
Formatted timestamp is : Jan 14, 1970 11:20:00 AM
IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
Formatted timestamp is : 1/14/70 11:20 AM
IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
Formatted timestamp is : 19700114 11:20 AM
------------

Input timestamp is : 2200000000
------------

IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
Formatted timestamp is : Sunday, September 18, 2039 1:06:40 PM GMT-10:00
IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
Formatted timestamp is : September 18, 2039 1:06:40 PM GMT-10:00
IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
Formatted timestamp is : Sep 18, 2039 1:06:40 PM
IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
Formatted timestamp is : 9/18/39 1:06 PM
IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
Formatted timestamp is : 20390918 01:06 PM
------------

Input timestamp is : -2200000000
------------

IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
Formatted timestamp is : Saturday, April 14, 1900 2:53:20 PM GMT-10:00
IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
Formatted timestamp is : April 14, 1900 2:53:20 PM GMT-10:00
IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
Formatted timestamp is : Apr 14, 1900 2:53:20 PM
IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
Formatted timestamp is : 4/14/00 2:53 PM
IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
Formatted timestamp is : 19000414 02:53 PM
------------

Input timestamp is : 90099999
------------

IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
Formatted timestamp is : Wednesday, November 8, 1972 9:46:39 AM GMT-10:00
IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
Formatted timestamp is : November 8, 1972 9:46:39 AM GMT-10:00
IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
Formatted timestamp is : Nov 8, 1972 9:46:39 AM
IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
Formatted timestamp is : 11/8/72 9:46 AM
IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
Formatted timestamp is : 19721108 09:46 AM
------------

Input timestamp is : 3600
------------

IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
Formatted timestamp is : Wednesday, December 31, 1969 3:00:00 PM GMT-10:00
IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
Formatted timestamp is : December 31, 1969 3:00:00 PM GMT-10:00
IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
Formatted timestamp is : Dec 31, 1969 3:00:00 PM
IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
Formatted timestamp is : 12/31/69 3:00 PM
IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
Formatted timestamp is : 19691231 03:00 PM
------------

Input timestamp is : -3600
------------

IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
Formatted timestamp is : Wednesday, December 31, 1969 1:00:00 PM GMT-10:00
IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
Formatted timestamp is : December 31, 1969 1:00:00 PM GMT-10:00
IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
Formatted timestamp is : Dec 31, 1969 1:00:00 PM
IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
Formatted timestamp is : 12/31/69 1:00 PM
IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
Formatted timestamp is : 19691231 01:00 PM
------------

Input localtime is : tm_sec : '24' , tm_min : '3' , tm_hour : '19' , tm_mday : '3' , tm_mon : '3' , tm_year : '105' ,
Input localtime is : tm_sec : '24' , tm_min : '3' , tm_hour : '19' , tm_mday : '3' , tm_mon : '3' , tm_year : '105' ,
------------

IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
Formatted localtime_array is : Sunday, April 3, 2005 7:03:24 PM GMT-10:00
IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
Formatted localtime_array is : April 3, 2005 7:03:24 PM GMT-10:00
IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
Formatted localtime_array is : Apr 3, 2005 7:03:24 PM
IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
Formatted localtime_array is : 4/3/05 7:03 PM
IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
Formatted localtime_array is : 20050403 07:03 PM
------------

Input localtime is : tm_sec : '21' , tm_min : '5' , tm_hour : '7' , tm_mday : '13' , tm_mon : '4' , tm_year : '205' ,
Input localtime is : tm_sec : '21' , tm_min : '5' , tm_hour : '7' , tm_mday : '13' , tm_mon : '4' , tm_year : '205' ,
------------

IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
Formatted localtime_array is : Wednesday, May 13, 2105 7:05:21 AM GMT-10:00
IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
Formatted localtime_array is : May 13, 2105 7:05:21 AM GMT-10:00
IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
Formatted localtime_array is : May 13, 2105 7:05:21 AM
IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
Formatted localtime_array is : 5/13/05 7:05 AM
IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
Formatted localtime_array is : 21050513 07:05 AM
------------

Input localtime is : tm_sec : '11' , tm_min : '13' , tm_hour : '0' , tm_mday : '17' , tm_mon : '11' , tm_year : '-5' ,
Input localtime is : tm_sec : '11' , tm_min : '13' , tm_hour : '0' , tm_mday : '17' , tm_mon : '11' , tm_year : '-5' ,
------------

IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
IntlDateFormatter locale= en_US ,datetype = 0 ,timetype =0
Formatted localtime_array is : Tuesday, December 17, 1895 12:13:11 AM GMT-10:00
IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
IntlDateFormatter locale= en_US ,datetype = 1 ,timetype =1
Formatted localtime_array is : December 17, 1895 12:13:11 AM GMT-10:00
IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
IntlDateFormatter locale= en_US ,datetype = 2 ,timetype =2
Formatted localtime_array is : Dec 17, 1895 12:13:11 AM
IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
IntlDateFormatter locale= en_US ,datetype = 3 ,timetype =3
Formatted localtime_array is : 12/17/95 12:13 AM
IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
IntlDateFormatter locale= en_US ,datetype = -1 ,timetype =-1
Formatted localtime_array is : 18951217 12:13 AM
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2010-01-01 01:02:03.000000',
'timezone_type' => 3,
'timezone' => 'UTC',
))
------------
Formatted DateTime is : Thursday, December 31, 2009 3:02:03 PM GMT-10:00
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2010-01-01 01:02:03.000000',
'timezone_type' => 3,
'timezone' => 'UTC',
))
------------
Formatted DateTime is : December 31, 2009 3:02:03 PM GMT-10:00
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2010-01-01 01:02:03.000000',
'timezone_type' => 3,
'timezone' => 'UTC',
))
------------
Formatted DateTime is : Dec 31, 2009 3:02:03 PM
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2010-01-01 01:02:03.000000',
'timezone_type' => 3,
'timezone' => 'UTC',
))
------------
Formatted DateTime is : 12/31/09 3:02 PM
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2010-01-01 01:02:03.000000',
'timezone_type' => 3,
'timezone' => 'UTC',
))
------------
Formatted DateTime is : 20091231 03:02 PM
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2000-12-30 19:04:05.000000',
'timezone_type' => 2,
'timezone' => 'PDT',
))
------------
Formatted DateTime is : Saturday, December 30, 2000 5:04:05 PM GMT-10:00
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2000-12-30 19:04:05.000000',
'timezone_type' => 2,
'timezone' => 'PDT',
))
------------
Formatted DateTime is : December 30, 2000 5:04:05 PM GMT-10:00
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2000-12-30 19:04:05.000000',
'timezone_type' => 2,
'timezone' => 'PDT',
))
------------
Formatted DateTime is : Dec 30, 2000 5:04:05 PM
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2000-12-30 19:04:05.000000',
'timezone_type' => 2,
'timezone' => 'PDT',
))
------------
Formatted DateTime is : 12/30/00 5:04 PM
------------
Date is: DateTime::__set_state(array(
Date is: \DateTime::__set_state(array(
'date' => '2000-12-30 19:04:05.000000',
'timezone_type' => 2,
'timezone' => 'PDT',
Expand Down
Loading

0 comments on commit a53ad90

Please sign in to comment.