Skip to content

Commit 3ff292c

Browse files
committed
PHPC-879: Remove dependency on php_date_get_immutable_ce()
1 parent 997fe40 commit 3ff292c

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

php_phongo.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ ZEND_DECLARE_MODULE_GLOBALS(mongodb)
7474
#endif
7575

7676
/* Declare zend_class_entry dependencies, which are initialized in MINIT */
77+
zend_class_entry *php_phongo_date_immutable_ce;
7778
zend_class_entry *php_phongo_json_serializable_ce;
7879

7980
php_phongo_server_description_type_map_t
@@ -1909,12 +1910,17 @@ PHP_MINIT_FUNCTION(mongodb)
19091910
*/
19101911

19111912
/* Initialize zend_class_entry dependencies.
1913+
*
1914+
* Although DateTimeImmutable was introduced in PHP 5.5.0,
1915+
* php_date_get_immutable_ce() is not available in PHP versions before
1916+
* 5.5.24 and 5.6.8.
19121917
*
19131918
* Although JsonSerializable was introduced in PHP 5.4.0,
19141919
* php_json_serializable_ce is not exported in PHP versions before 5.4.26
19151920
* and 5.5.10. For later PHP versions, looking up the class manually also
19161921
* helps with distros that disable LTDL_LAZY for dlopen() (e.g. Fedora).
19171922
*/
1923+
php_phongo_date_immutable_ce = php_phongo_fetch_internal_class(ZEND_STRL("datetimeimmutable") TSRMLS_CC);
19181924
php_phongo_json_serializable_ce = php_phongo_fetch_internal_class(ZEND_STRL("jsonserializable") TSRMLS_CC);
19191925

19201926
if (php_phongo_json_serializable_ce == NULL) {

php_phongo_classes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "php_phongo_structs.h"
2121

2222
/* Export zend_class_entry dependencies, which are initialized in MINIT */
23+
extern zend_class_entry *php_phongo_date_immutable_ce;
2324
extern zend_class_entry *php_phongo_json_serializable_ce;
2425

2526
#if PHP_VERSION_ID >= 70000

src/BSON/UTCDateTime.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,9 @@ PHP_METHOD(UTCDateTime, __construct)
176176
}
177177

178178
if (Z_TYPE_P(milliseconds) == IS_OBJECT) {
179-
if (instanceof_function(Z_OBJCE_P(milliseconds), php_date_get_date_ce() TSRMLS_CC)) {
179+
if (instanceof_function(Z_OBJCE_P(milliseconds), php_date_get_date_ce() TSRMLS_CC) ||
180+
(php_phongo_date_immutable_ce && instanceof_function(Z_OBJCE_P(milliseconds), php_phongo_date_immutable_ce TSRMLS_CC))) {
180181
php_phongo_utcdatetime_init_from_date(intern, Z_PHPDATE_P(milliseconds));
181-
#if PHP_VERSION_ID >= 50500
182-
} else if (instanceof_function(Z_OBJCE_P(milliseconds), php_date_get_immutable_ce() TSRMLS_CC)) {
183-
php_phongo_utcdatetime_init_from_date(intern, Z_PHPDATE_P(milliseconds));
184-
#endif
185182
} else {
186183
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT TSRMLS_CC, "Expected instance of DateTimeInterface, %s given", ZSTR_VAL(Z_OBJCE_P(milliseconds)->name));
187184
}

0 commit comments

Comments
 (0)