简体   繁体   中英

Writing to DatePeriod properties is unsupported?

I decided to do an update on the PHP version I use, from 5.4.7 to 5.4.30. It was quite a surprise to see that some scripts extending DatePeriod started throwing error messages.

Here is a sample of code from the bug report mentioned later.

<?php
class Period extends \DatePeriod
{
    public $test;
}

$p = new Period(new \DateTime('now'), new \DateInterval('P1Y'), new \DateTime('tomorrow'));
$p->test = false;

var_dump($p);

Surprisingly a search after the error message:

Writing to DatePeriod properties is unsupported

returned almost no useful results, just the bug report Bug #65672 Broken classes inherited from DatePeriod .

Am I missing something here?

Wow, what a change to the code. Unfortunately you're not missing anything. The fix for bug 53437 was very poorly thought out and broke backwards compatibility. I looked at the patch that was applied - all write access to properties from userland code is indeed strictly forbidden. This patch should not have been accepted into PHP, and this restriction should be reverted while also re-engineering a fix for the original bug.

You will not find a workaround for extending the DatePeriod class, and the problem is unlikely to be patched anytime soon. It would appear your only option is to implement your additional functionality using a wrapper class rather than inheriting directly from DatePeriod.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM