Skip to content

Conversation

@davedevelopment
Copy link

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 of DateTimeImmutable. The existing DateTimeImmutable::createFromMutable method doesn't really help me much here, so I figured we could just have a createFrom method. I added a corresponding DateTime::createFrom for consistency.

<?php
// We want to be liberal in what we accept
function foo(DateTimeInterface $input) {

   // but for our processing we want $input to be immutable

   // Currently
   $input = $input instanceof DateTimeImmutable
       ? $input
       : DateTimeImmutable::createFromMutable($input);

   // Proposed DateTimeImmutable::createFrom(DateTimeInterface $input)
   $input = DateTimeImmutable::createFrom($input);

   // ...
}

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.

@Majkl578
Copy link
Contributor

Read here why I decided against type hinting DateTimeInterface in DateTime::createFromImmutable().

@davedevelopment
Copy link
Author

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 DateTimeImmutable::createFromMutable and DateTime::createFromImmutable?

@Majkl578
Copy link
Contributor

I think they are, DateTimeImmutable::createFrom(new DateTimeImmutable()) / DateTime::createFrom(new DateTime()) are still simply cloning.

@smalyshev smalyshev requested a review from derickr August 26, 2018 23:45
Copy link
Member

@derickr derickr left a 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.

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)
Copy link
Member

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.

new_obj->time = timelib_time_clone(old_obj->time);
}
/* }}} */
/* {{{ proto DateTime::createFrom(DateTimeInterface object)
Copy link
Member

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.

/* }}} */
/* {{{ proto DateTime::createFrom(DateTimeInterface object)
Creates new DateTime object from an existing DateTimeInterface object.
*/
Copy link
Member

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.
*/
Copy link
Member

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
Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

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

"Test"

}
..and get names of all its methods
array(19) {
array(20) {
Copy link
Member

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.

@cmb69
Copy link
Member

cmb69 commented Jan 3, 2020

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.

@cmb69 cmb69 closed this Jan 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants