-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add methods DateTime::createFrom and DateTimeImmutable::createFrom #3394
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
Add methods DateTime::createFrom and DateTimeImmutable::createFrom #3394
Conversation
Read here why I decided against type hinting DateTimeInterface in |
I'm not sure if those arguments are relevant as I'm not proposing touching the interface here, just making slightly more generic versions of the existing methods |
I think they are, |
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.
I think the code is fine, minus my coding standard nit picks. However, I do think we need to think hard about the naming, and naming is hard. I'd suggest that for that bit, and to find out whether we actually want to have these functions (I think yes), we should have an RFC.
@@ -521,6 +526,7 @@ static const zend_function_entry date_funcs_immutable[] = { | |||
PHP_ME(DateTimeImmutable, setISODate, arginfo_date_method_isodate_set, 0) | |||
PHP_ME(DateTimeImmutable, setTimestamp, arginfo_date_method_timestamp_set, 0) | |||
PHP_ME(DateTimeImmutable, createFromMutable, arginfo_date_method_create_from_mutable, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) | |||
PHP_ME(DateTimeImmutable, createFrom , arginfo_date_method_create_from, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) |
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.
The comma needs to go immediately after createFrom
.
@@ -2901,6 +2907,47 @@ PHP_METHOD(DateTimeImmutable, createFromMutable) | |||
new_obj->time = timelib_time_clone(old_obj->time); | |||
} | |||
/* }}} */ | |||
/* {{{ proto DateTime::createFrom(DateTimeInterface object) |
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.
Add an empty line in between methods please.
@@ -2901,6 +2907,47 @@ PHP_METHOD(DateTimeImmutable, createFromMutable) | |||
new_obj->time = timelib_time_clone(old_obj->time); | |||
} | |||
/* }}} */ | |||
/* {{{ proto DateTime::createFrom(DateTimeInterface object) | |||
Creates new DateTime object from an existing DateTimeInterface object. | |||
*/ |
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.
The closing */
needs to go at the end of the previous line.
|
||
/* {{{ proto DateTimeImmutable::createFrom(DateTimeInterface object) | ||
Creates new DateTimeImmutable object from an existing DateTimeInterface object. | ||
*/ |
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.
The closing */
needs to go at the end of the previous line.
@@ -0,0 +1,31 @@ | |||
--TEST-- | |||
Tests for DateTimeImmutable::createFrom |
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.
"Test"
@@ -0,0 +1,31 @@ | |||
--TEST-- | |||
Tests for DateTime::createFrom |
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.
"Test"
@@ -32,7 +32,7 @@ object(ReflectionClass)#%d (1) { | |||
string(8) "DateTime" | |||
} | |||
..and get names of all its methods | |||
array(19) { | |||
array(20) { |
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.
Ugh, I hate this test. It adds nothing. I'd delete it.
Since PR #5016 has been merged, I'm closing this PR. Thanks for working on this @davedevelopment, and sorry that the PR apparently has been forgotten. |
I like to write methods that are liberal and will accept a
DateTimeInterface
, but quite often I explicitly want to work with the input as instances ofDateTimeImmutable
. The existingDateTimeImmutable::createFromMutable
method doesn't really help me much here, so I figured we could just have acreateFrom
method. I added a correspondingDateTime::createFrom
for consistency.I'm not overly familiar with php internals, but I cobbled this patch together. I'm also not too sure if new methods like this require an RFC. An RFC seems a little overkill, but will look to put one together if needs be.