1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00
Files
archived-php-src/Zend/tests/readonly_props/promotion.phpt
Nikita Popov 6780aaa532 Implement readonly properties
Add support for readonly properties, for which only a single
initializing assignment from the declaring scope is allowed.

RFC: https://wiki.php.net/rfc/readonly_properties_v2

Closes GH-7089.
2021-07-20 12:05:46 +02:00

42 lines
619 B
PHP

--TEST--
Promoted readonly property
--FILE--
<?php
class Point {
public function __construct(
public readonly float $x = 0.0,
public readonly float $y = 0.0,
public readonly float $z = 0.0,
) {}
}
var_dump(new Point);
$point = new Point(1.0, 2.0, 3.0);
try {
$point->x = 4.0;
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
var_dump($point);
?>
--EXPECT--
object(Point)#1 (3) {
["x"]=>
float(0)
["y"]=>
float(0)
["z"]=>
float(0)
}
Cannot modify readonly property Point::$x
object(Point)#1 (3) {
["x"]=>
float(1)
["y"]=>
float(2)
["z"]=>
float(3)
}