php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76790 When inheriting DateTimeImmutable, the return value type is different depending
Submitted: 2018-08-24 05:37 UTC Modified: 2020-04-11 13:04 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: phper dot 0o0 at gmail dot com Assigned: derick (profile)
Status: Not a bug Package: Date/time related
PHP Version: 7.1.21 OS: All
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: phper dot 0o0 at gmail dot com
New email:
PHP Version: OS:

 

 [2018-08-24 05:37 UTC] phper dot 0o0 at gmail dot com
Description:
------------
The following code will return `DateTimeImmutable` when using the `createFromFormat` method.


However, the following code will return `ChildDateTimeImmutable` when `setDate` is executed.

Test script:
---------------
<?php
class ChildDateTimeImmutable extends DateTimeImmutable
{
}

$child = new ChildDateTimeImmutable('2018-08-01');
assert($child->createFromFormat('Y-m-d', '2018-01-01') instanceof ChildDateTimeImmutable);
assert($child->setDate(2018, 1, 1) instanceof ChildDateTimeImmutable);

Expected result:
----------------
I expectation is to return `ChildDateTimeImmutable` when using` createFromFormat`.

Actual result:
--------------
Warning: assert(): assert($child->createFromFormat('Y-m-d', '2018-01-01') instanceof ChildDateTimeImmutable) failed in /private/tmp/20180824140235.php on line 7

Call Stack:
    0.0009     361408   1. {main}() /private/tmp/20180824140235.php:0
    0.0049     362504   2. assert() /private/tmp/20180824140235.php:7

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-08-27 09:44 UTC] cmb@php.net
-Package: *General Issues +Package: Date/time related
 [2020-01-20 17:37 UTC] girgias@php.net
-Assigned To: +Assigned To: derick
 [2020-01-20 17:37 UTC] girgias@php.net
Is this the expected behaviour Derick?
 [2020-04-11 13:04 UTC] cmb@php.net
-Status: Assigned +Status: Not a bug
 [2020-04-11 13:04 UTC] cmb@php.net
This behavior is to be expected, because ::createFromFormat() is a
*static* method, and as such is not even aware of the object it is
called upon.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 20:01:31 2024 UTC