-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add factory methods for DatePeriod and ReflectionMethod #6754
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
Conversation
ZVAL_COPY_VALUE(return_value, &object); | ||
} | ||
|
||
PHP_METHOD(DatePeriod, createFromDates) |
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.
other candidates I thought about: createBetweenDates
, createBetweenStartAndEnd
, createFromStartAndEnd
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 I would prefer createFromStartAndEnd
, and I would also switch the order of arguments to:
public static function createFromStartAndEnd(DateTimeInterface $start, DateTimeInterface $end, DateInterval $interval, int $options = 0): 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.
Another suggestion would be to use createFromEndpoints
or createFromDatepoints
to imply that the DatePeriod
endpoints are expressed using DateTimeInterface
objects.
@@ -398,6 +398,12 @@ public static function __set_state(array $array) {} | |||
|
|||
class DatePeriod implements IteratorAggregate |
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 we should decide what to do with the overloaded constructor:
- do nothing so that the overloaded signature remains supported forever
- deprecate it in 8.x and remove it in 9.0 altogether
- choose one "canonical" signature, deprecate the others in 8.x, and get rid of them in 9.0
Depending on the outcome, we could only have 2 factory methods, and the 3rd signature could be implemented by the constructor
@nikic Now that there is an implementation for these methods... Do you think it's a good idea to vote about their inclusion in PHP 8.1 and the deprecation of the related constructor behavior in PHP 8.2 as part of the 8.1 Deprecations RFC? |
929d5b3
to
640830c
Compare
size_t isostr_len = 0; | ||
static void construct_date_period( | ||
INTERNAL_FUNCTION_PARAMETERS, zval *object, zval *start, zval *end, zval *interval, zend_long recurrences, | ||
zend_long options, char *isostr, size_t isostr_len |
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 would prefer it if this initialisation function gets split as well. The old __construct
should use the new (3) initialisation methods, which also makes it easier to clean up in for example PHP 9.0. It can also mean that error and bounds checking (for example, recurrences can't be negative) to the right and logical position.
@kocsismate Are there plans to pursue this RFC? |
@iluuu1994 yes, absolutely! My plan is to first fix the easier issues in stubs (mostly declaring the missing constants) and then continue with the improvements which need more thinking. |
My suggestions for the methods:
I'm pretty sure that the static methods are in order of popularity as written here, although perhaps that can be researched (though constructor overloads make this problematic). In addition to that, having the string constructor be the main default one, and having a __toString method would enable easy and convenient serializing/unserializing to/from database. P.S. I don't understand why including the end date is opt-in; to me it makes far more sense for it to be opt-out, same as EXCLUDE_START_DATE. |
Closing in favor of #11703 |
This PR proposes to add the following factory methods:
The longer-term advantage of these added methods is that we could get rid of the overloaded signatures of
DatePeriod::__construct()
andReflectionMethod::__construct()
in PHP 9 after a deprecation phase in PHP 8.x (https://wiki.php.net/rfc/deprecations_php_8_1#dateperiodconstruct and https://wiki.php.net/rfc/deprecations_php_8_1#passing_a_method_name_as_the_first_parameter_to_reflectionmethodconstruct).P.S. I don't have any strong preference about the method names, and the current ones should be considered as WIP ones. :)