Skip to content

Fixes GH-8152: add __serialise and __unserialise methods to DateTime and DateTimeZone #8422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

derickr
Copy link
Member

@derickr derickr commented Apr 22, 2022

This adds the __serialize() and unserialize() methods to DateTime, DateTimeImmutable, and DateTimeZone for PHP 8.2 and later.

This specific PR does not touch on DateInterval and DatePeriod yet, because these cases are more complicated. I will make a separate PR, which might need to end up breaking BC (albeit for functionality that didn't work as expected anyway).

/cc @nicolas-grekas

Copy link
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the return type of the function which needs to be changed from zend_result to bool LGTM, will leave some time for Nicolas to also have a look.

@@ -3407,19 +3501,19 @@ static zend_result timezone_initialize(php_timezone_obj *tzobj, const char *tz,
if (strlen(tz) != tz_len) {
php_error_docref(NULL, E_WARNING, "Timezone must not contain null bytes");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a test to see if a crafted serialisation will trigger this warning?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, but will check and add one if needed.

Comment on lines +1841 to +1866
switch (purpose) {
case ZEND_PROP_PURPOSE_DEBUG:
case ZEND_PROP_PURPOSE_SERIALIZE:
case ZEND_PROP_PURPOSE_VAR_EXPORT:
case ZEND_PROP_PURPOSE_JSON:
case ZEND_PROP_PURPOSE_ARRAY_CAST:
break;
default:
return zend_std_get_properties_for(object, purpose);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An idea for later, it seems similar code to this is present in other functions, maybe extract this into it's own function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is something you need in nearly every serialise handler, not only in the Date/Time ones. It's a little tricky to extract, as it has both a break (do nothing) and has to return something. I'll make a note and see if this can be done better at some point.

Comment on lines 2646 to 2670
dateobj = Z_PHPDATE_P(object);

array_init(return_value);
myht = Z_ARRVAL_P(return_value);
date_object_to_hash(dateobj, myht);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can crash if the __constructor was not called:

class D extends DateTime { public function __construct() {} }

serialize(new D());

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it can, as the __serialize handler for "D" does not call the __serialize handler for "DateTime".

However, the following does crash:

<?php
class D extends DateTime {
	public function __construct() {}
	public function __serialize() : array {
		return parent::__serialize();
	}
}
echo serialize(new D());

Will add a guard, good catch!

@nicolas-grekas
Copy link
Contributor

nicolas-grekas commented Apr 25, 2022

This works nicely with symfony/var-exporter, thanks!

@derickr derickr force-pushed the GH-8152-add-serialise-and-unserialise-datetime-and-datetimezone branch from 97db8b3 to 6f2c501 Compare April 29, 2022 08:44
@derickr derickr merged commit b46632e into php:master Apr 29, 2022
nicolas-grekas added a commit to symfony/symfony that referenced this pull request Apr 30, 2022
…icolas-grekas)

This PR was merged into the 4.4 branch.

Discussion
----------

[VarExporter] Fix exporting DateTime objects on PHP 8.2

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Needs php/php-src#8422

Commits
-------

a09e2b4 [VarExporter] Fix exporting DateTime objects on PHP 8.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants