[PHP-CVS] [php-src] master: date: Correctly handle `Duration::__construct()` calls (#23098)

[email protected] (Tim Düsterhus via GitHub)
Newsgroups php.cvs
Message-ID <[email protected]>
Author: Tim Düsterhus (TimWolla)
Committer: GitHub (web-flow)
Pusher: TimWolla
Date: 2026-08-07T13:59:38+02:00

Commit: https://github.com/php/php-src/commit/d22965337cd8e5ced274d6e7a2cfd07593e5efbf
Raw diff: https://github.com/php/php-src/commit/d22965337cd8e5ced274d6e7a2cfd07593e5efbf.diff

date: Correctly handle `Duration::__construct()` calls (#23098)

Co-authored-by: Nora Dossche <[email protected]>

Changed paths:
  M  ext/date/tests/time/duration/readonly.phpt
  M  ext/date/time_duration.c


Diff:

diff --git a/ext/date/tests/time/duration/readonly.phpt b/ext/date/tests/time/duration/readonly.phpt
index 36b407138fee..ca527fe43c59 100644
--- a/ext/date/tests/time/duration/readonly.phpt
+++ b/ext/date/tests/time/duration/readonly.phpt
@@ -89,6 +89,16 @@ echo "====", PHP_EOL;
 
 var_dump((new ReflectionProperty($d, 'seconds'))->isWritable(null, $d));
 
+echo "====", PHP_EOL;
+
+try {
+    (new ReflectionMethod($d, '__construct'))->invoke($d);
+} catch (Throwable $e) {
+    echo $e::class, ': ', $e->getMessage(), PHP_EOL;
+}
+
+echo f($d), PHP_EOL;
+
 ?>
 --EXPECT--
 Error: Cannot modify readonly property Time\Duration::$seconds
@@ -112,3 +122,6 @@ Error: Cannot modify readonly property Time\Duration::$seconds
 Error: Cannot modify protected(set) readonly property Time\Duration::$seconds from global scope
 ====
 bool(false)
+====
+Error: Cannot directly construct Time\Duration, use Time\Duration::from*() methods instead
+         +1.000000000
diff --git a/ext/date/time_duration.c b/ext/date/time_duration.c
index d9952a829d2c..14c121ff29bb 100644
--- a/ext/date/time_duration.c
+++ b/ext/date/time_duration.c
@@ -146,6 +146,7 @@ ZEND_ATTRIBUTE_NODISCARD static zend_result create_duration(zval *target, zend_u
 
 PHP_METHOD(Time_Duration, __construct)
 {
+	zend_throw_error(NULL, "Cannot directly construct Time\\Duration, use Time\\Duration::from*() methods instead");
 }
 
 PHP_METHOD(Time_Duration, fromSeconds)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.