-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Added method DateTime::createFromImmutable() [follow-up for #1145] #2484
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,7 @@ PHP_FUNCTION(getdate); | |
PHP_METHOD(DateTime, __construct); | ||
PHP_METHOD(DateTime, __wakeup); | ||
PHP_METHOD(DateTime, __set_state); | ||
PHP_METHOD(DateTime, createFromImmutable); | ||
PHP_FUNCTION(date_create); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would say There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True, but there is already DateTimeImmutable::createFromMutable(), so the naming is chosen mostly for consistency. :/ |
||
PHP_FUNCTION(date_create_immutable); | ||
PHP_FUNCTION(date_create_from_format); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--TEST-- | ||
Tests for DateTime::createFromImmutable. | ||
--INI-- | ||
date.timezone=Europe/London | ||
--FILE-- | ||
<?php | ||
$current = "2014-03-02 16:24:08"; | ||
$i = date_create_immutable( $current ); | ||
|
||
$m = DateTime::createFromImmutable( $i ); | ||
var_dump( $m ); | ||
|
||
$m->modify('+ 1 hour'); | ||
|
||
var_dump( $i->format('Y-m-d H:i:s') === $current ); | ||
|
||
$m = DateTime::createFromImmutable( date_create( $current ) ); | ||
var_dump( $m ); | ||
?> | ||
--EXPECTF-- | ||
object(DateTime)#%d (3) { | ||
["date"]=> | ||
string(26) "2014-03-02 16:24:08.000000" | ||
["timezone_type"]=> | ||
int(3) | ||
["timezone"]=> | ||
string(13) "Europe/London" | ||
} | ||
bool(true) | ||
|
||
Warning: DateTime::createFromImmutable() expects parameter 1 to be DateTimeImmutable, object given in %stests%eDateTime_createFromImmutable.php on line %d | ||
NULL |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ object(ReflectionClass)#%d (1) { | |
string(8) "DateTime" | ||
} | ||
..and get names of all its methods | ||
array(18) { | ||
array(19) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the point of this test? (I realise, it has nothing to do with the PR). I'd say we remove it as it just tests that Reflection works. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was asking the same question myself, but just cowardly changed the expectf, because I didn't want to remove tests in the PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is extremely important, since reflection of internal classes is a constant SNAFU |
||
[0]=> | ||
object(ReflectionMethod)#%d (2) { | ||
["name"]=> | ||
|
@@ -52,102 +52,109 @@ array(18) { | |
[3]=> | ||
object(ReflectionMethod)#%d (2) { | ||
["name"]=> | ||
string(16) "createFromFormat" | ||
string(19) "createFromImmutable" | ||
["class"]=> | ||
string(8) "DateTime" | ||
} | ||
[4]=> | ||
object(ReflectionMethod)#%d (2) { | ||
["name"]=> | ||
string(13) "getLastErrors" | ||
string(16) "createFromFormat" | ||
["class"]=> | ||
string(8) "DateTime" | ||
} | ||
[5]=> | ||
object(ReflectionMethod)#%d (2) { | ||
["name"]=> | ||
string(6) "format" | ||
string(13) "getLastErrors" | ||
["class"]=> | ||
string(8) "DateTime" | ||
} | ||
[6]=> | ||
object(ReflectionMethod)#%d (2) { | ||
["name"]=> | ||
string(6) "modify" | ||
string(6) "format" | ||
["class"]=> | ||
string(8) "DateTime" | ||
} | ||
[7]=> | ||
object(ReflectionMethod)#%d (2) { | ||
["name"]=> | ||
string(3) "add" | ||
string(6) "modify" | ||
["class"]=> | ||
string(8) "DateTime" | ||
} | ||
[8]=> | ||
object(ReflectionMethod)#%d (2) { | ||
["name"]=> | ||
string(3) "sub" | ||
string(3) "add" | ||
["class"]=> | ||
string(8) "DateTime" | ||
} | ||
[9]=> | ||
object(ReflectionMethod)#%d (2) { | ||
["name"]=> | ||
string(11) "getTimezone" | ||
string(3) "sub" | ||
["class"]=> | ||
string(8) "DateTime" | ||
} | ||
[10]=> | ||
object(ReflectionMethod)#%d (2) { | ||
["name"]=> | ||
string(11) "setTimezone" | ||
string(11) "getTimezone" | ||
["class"]=> | ||
string(8) "DateTime" | ||
} | ||
[11]=> | ||
object(ReflectionMethod)#%d (2) { | ||
["name"]=> | ||
string(9) "getOffset" | ||
string(11) "setTimezone" | ||
["class"]=> | ||
string(8) "DateTime" | ||
} | ||
[12]=> | ||
object(ReflectionMethod)#%d (2) { | ||
["name"]=> | ||
string(7) "setTime" | ||
string(9) "getOffset" | ||
["class"]=> | ||
string(8) "DateTime" | ||
} | ||
[13]=> | ||
object(ReflectionMethod)#%d (2) { | ||
["name"]=> | ||
string(7) "setDate" | ||
string(7) "setTime" | ||
["class"]=> | ||
string(8) "DateTime" | ||
} | ||
[14]=> | ||
object(ReflectionMethod)#%d (2) { | ||
["name"]=> | ||
string(10) "setISODate" | ||
string(7) "setDate" | ||
["class"]=> | ||
string(8) "DateTime" | ||
} | ||
[15]=> | ||
object(ReflectionMethod)#%d (2) { | ||
["name"]=> | ||
string(12) "setTimestamp" | ||
string(10) "setISODate" | ||
["class"]=> | ||
string(8) "DateTime" | ||
} | ||
[16]=> | ||
object(ReflectionMethod)#%d (2) { | ||
["name"]=> | ||
string(12) "getTimestamp" | ||
string(12) "setTimestamp" | ||
["class"]=> | ||
string(8) "DateTime" | ||
} | ||
[17]=> | ||
object(ReflectionMethod)#%d (2) { | ||
["name"]=> | ||
string(12) "getTimestamp" | ||
["class"]=> | ||
string(8) "DateTime" | ||
} | ||
[18]=> | ||
object(ReflectionMethod)#%d (2) { | ||
["name"]=> | ||
string(4) "diff" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it not make more sense to group this with the other DateTime methods, instead of just before the DateTimeImmutable/createFromMutable method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where exactly should I put it then? There are __construct for mutable & immutable together, __set_state for both together as well...