Skip to content

[Rfc] data classes #16904

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Zend/tests/data_class_001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--TEST--
Data classes can be defined and compared.
--FILE--
<?php

data class Point {
public function __construct(public int $x, public int $y) {}
}

$p1 = new Point(1, 2);
$p2 = new Point(1, 2);
$p3 = new Point(2, 3);

var_dump($p1 === $p2);
var_dump($p1 !== $p2);
var_dump($p1 === $p3);
var_dump($p1 !== $p3);
var_dump($p1);
?>
--EXPECT--
bool(true)
bool(false)
bool(false)
bool(true)
data object(Point)#1 (2) {
["x"]=>
int(1)
["y"]=>
int(2)
}
29 changes: 29 additions & 0 deletions Zend/tests/data_class_002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--TEST--
Data classes can be combined with inheritance and other features.
--FILE--
<?php

data class Point {
public function __construct(public int $x, public int $y) {}
}

final data class Point3D extends Point {
public function __construct(int $x, int $y, public int $z) {
parent::__construct($x, $y);
}
}

$p2 = new Point3D(1, 2, 3);
$p3 = new Point3D(1, 2, 3);
$p4 = new Point3D(2, 3, 4);

var_dump($p2 === $p3);
var_dump($p2 !== $p3);
var_dump($p2 === $p4);
var_dump($p2 !== $p4);
?>
--EXPECT--
bool(true)
bool(false)
bool(false)
bool(true)
17 changes: 17 additions & 0 deletions Zend/tests/data_class_003.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
Data class inheritance rules are enforced.
--FILE--
<?php

class Point {
public function __construct(public int $x, public int $y) {}
}

data class Point3D extends Point {
public function __construct(int $x, int $y, public int $z) {
parent::__construct($x, $y);
}
}
?>
--EXPECTF--
Fatal error: Data class Point3D cannot extend non-data class Point in %s on line %d
21 changes: 21 additions & 0 deletions Zend/tests/data_class_004.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Reflection should work
--FILE--
<?php

class Foo {
public function bar() {}
}

data class Bar {
public function baz() {}
}

$foo = new ReflectionClass(Foo::class);
$bar = new ReflectionClass(Bar::class);
var_dump($foo->isDataClass());
var_dump($bar->isDataClass());
?>
--EXPECT--
bool(false)
bool(true)
52 changes: 52 additions & 0 deletions Zend/tests/data_class_005.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
--TEST--
Copy on write
--FILE--
<?php

data class Point {
public function __construct(public int $x, public int $y) {}
public function add(Point $other): Point {
$this->x += $other->x;
$this->y += $other->y;
return $this;
}
public function __toString(): string {
return "({$this->x},{$this->y})";
}
}

$p1 = new Point(1, 2);
$p2 = $p1;

// p2 is copy on write (3,2)
$p2->x = 3;
echo "p1: $p1\n";
echo "p2 (x+3): $p2\n";

echo "p1->add(p2): " . $p1->add($p2) . "\n";
echo "p1: $p1\n";
$po = new Point(1,1);
$p2 = $po;
$p2->x++;
echo "p2: $p2\n";
echo "po: $po\n";

function modify(Point $p): Point {
$p->y++;
return $p;
}

$p1 = new Point(1,1);
$p2 = modify($p1);
echo "p1: $p1\n";
echo "p2: $p2\n";

--EXPECT--
p1: (1,2)
p2 (x+3): (3,2)
p1->add(p2): (4,4)
p1: (1,2)
p2: (2,1)
po: (1,1)
p1: (1,1)
p2: (1,2)
42 changes: 42 additions & 0 deletions Zend/tests/data_class_006.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--
A convoluted path
--FILE--
<?php

data class Length {
public function __construct(public float $length) {}
}

data class Point {
public int $x;
private Length $l;
public function __construct(int $x, public int $y) {
$previous = $this;
$this->x = $x;
$this->l = new Length($x + $y);
echo "$previous === $this\n";
}

public function addNoisily(Point $other): Point {
echo "Adding $this and $other\n";
$previous = $this;
$this->x += $other->x;
$this->y += $other->y;
$this->l = new Length($this->x + $this->y);
echo "$previous !== $this\n";
return $this;
}

public function __toString(): string {
return "({$this->x},{$this->y}|{$this->l->length})";
}
}

$p1 = new Point(1, 2);
$p2 = $p1->addNoisily($p1);

?>
--EXPECT--
(1,2|3) === (1,2|3)
Adding (1,2|3) and (1,2|3)
(1,2|3) !== (2,4|6)
18 changes: 18 additions & 0 deletions Zend/tests/data_class_007.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Private properties are included in equality checks
--FILE--
<?php

data class Length {
public function __construct(private float $length) {}
}

$l1 = new Length(1.0);
$l2 = new Length(1.0);
$l3 = new Length(2.0);
var_dump($l1 === $l2);
var_dump($l1 !== $l3);
?>
--EXPECT--
bool(true)
bool(true)
16 changes: 16 additions & 0 deletions Zend/tests/data_class_008.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Data classes can be anonymous
--FILE--
<?php

$data = new data class(1) {
public function __construct(private int $value) {}
};

var_dump($data);
?>
--EXPECT--
data object(class@anonymous)#1 (1) {
["value":"class@anonymous":private]=>
int(1)
}
20 changes: 20 additions & 0 deletions Zend/tests/data_class_009.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
unset triggers copy-on-write
--FILE--
<?php

$data = new data class(1) {
public function __construct(public int $value) {}
};

$copy = $data;
unset($copy->value);
var_dump($data !== $copy);
var_dump($data);
?>
--EXPECT--
bool(true)
data object(class@anonymous)#1 (1) {
["value"]=>
int(1)
}
18 changes: 18 additions & 0 deletions Zend/tests/data_class_010.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Serialization works
--FILE--
<?php

data class Point {
public function __construct(public int $x, public int $y) {}
}

$p = new Point(1, 2);
$s = serialize($p);
var_dump($s);
$p2 = unserialize($s);
var_dump($p2 === $p);
?>
--EXPECT--
string(40) "O:5:"Point":2:{s:1:"x";i:1;s:1:"y";i:2;}"
bool(true)
37 changes: 37 additions & 0 deletions Zend/tests/data_class_011.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
--TEST--
Other features work with data classes
--FILE--
<?php

interface Point {
public function add(Point $point): Point;
public float $length { get; }
}

trait PythagoreanTheorem {
public readonly float $length;

public function memoizeLength(): void {
$this->length = sqrt($this->x ** 2 + $this->y ** 2);
}
}

final readonly data class Point2D implements Point {
use PythagoreanTheorem; // contains implementation of $length

public function __construct(public int $x, public int $y) {
$this->memoizeLength(); // from the trait
}

public function add(Point $point): Point {
return new static($this->x + $point->x, $this->y + $point->y);
}
}

$p1 = new Point2D(1, 1);
$p2 = new Point2D(2, 2);
$p3 = $p1->add($p2);
echo $p3->length, "\n";
?>
--EXPECT--
6
3 changes: 3 additions & 0 deletions Zend/zend_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -1898,6 +1898,9 @@ static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int prio
if (decl->flags & ZEND_ACC_READONLY_CLASS) {
smart_str_appends(str, "readonly ");
}
if (decl->flags & ZEND_ACC_DATA_CLASS) {
smart_str_appends(str, "data ");
}
smart_str_appends(str, "class ");
}
smart_str_appendl(str, ZSTR_VAL(decl->name), ZSTR_LEN(decl->name));
Expand Down
5 changes: 5 additions & 0 deletions Zend/zend_attributes.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ static void validate_allow_dynamic_properties(
ZSTR_VAL(scope->name)
);
}
if (scope->ce_flags & ZEND_ACC_DATA_CLASS) {
zend_error_noreturn(E_ERROR, "Cannot apply #[AllowDynamicProperties] to data class %s",
ZSTR_VAL(scope->name)
);
}
if (scope->ce_flags & ZEND_ACC_ENUM) {
zend_error_noreturn(E_ERROR, "Cannot apply #[AllowDynamicProperties] to enum %s",
ZSTR_VAL(scope->name)
Expand Down
10 changes: 10 additions & 0 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,8 @@ static const char *zend_modifier_token_to_string(uint32_t token)
return "final";
case T_READONLY:
return "readonly";
case T_DATA:
return "data";
case T_ABSTRACT:
return "abstract";
case T_PUBLIC_SET:
Expand Down Expand Up @@ -995,6 +997,10 @@ uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag) /* {{{ */
zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0);
return 0;
}
if ((flags & ZEND_ACC_DATA_CLASS) && (new_flag & ZEND_ACC_DATA_CLASS)) {
zend_throw_exception(zend_ce_compile_error, "Multiple data modifiers are not allowed", 0);
return 0;
}
if ((new_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) && (new_flags & ZEND_ACC_FINAL)) {
zend_throw_exception(zend_ce_compile_error,
"Cannot use the final modifier on an abstract class", 0);
Expand All @@ -1020,6 +1026,10 @@ uint32_t zend_add_anonymous_class_modifier(uint32_t flags, uint32_t new_flag)
zend_throw_exception(zend_ce_compile_error, "Multiple readonly modifiers are not allowed", 0);
return 0;
}
if ((flags & ZEND_ACC_DATA_CLASS) && (new_flag & ZEND_ACC_DATA_CLASS)) {
zend_throw_exception(zend_ce_compile_error, "Multiple data modifiers are not allowed", 0);
return 0;
}
return new_flags;
}

Expand Down
5 changes: 4 additions & 1 deletion Zend/zend_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ typedef struct _zend_oparray_context {
#define ZEND_ACC_PROTECTED_SET (1 << 11) /* | | X | */
#define ZEND_ACC_PRIVATE_SET (1 << 12) /* | | X | */
/* | | | */
/* Class Flags (unused: 30,31) | | | */
/* Class Flags (unused: 31) | | | */
/* =========== | | | */
/* | | | */
/* Special class types | | | */
Expand Down Expand Up @@ -333,6 +333,9 @@ typedef struct _zend_oparray_context {
/* Class cannot be serialized or unserialized | | | */
#define ZEND_ACC_NOT_SERIALIZABLE (1 << 29) /* X | | | */
/* | | | */
/* Class is compared by value | | | */
#define ZEND_ACC_DATA_CLASS (1 << 30) /* X | | | */
/* | | | */
/* Function Flags (unused: 29-30) | | | */
/* ============== | | | */
/* | | | */
Expand Down
7 changes: 7 additions & 0 deletions Zend/zend_inheritance.c
Original file line number Diff line number Diff line change
Expand Up @@ -1783,6 +1783,13 @@ ZEND_API void zend_do_inheritance_ex(zend_class_entry *ce, zend_class_entry *par
);
}

if (UNEXPECTED((ce->ce_flags & ZEND_ACC_DATA_CLASS) != (parent_ce->ce_flags & ZEND_ACC_DATA_CLASS))) {
zend_error_noreturn(E_COMPILE_ERROR, "%s class %s cannot extend %s class %s",
ce->ce_flags & ZEND_ACC_DATA_CLASS ? "Data" : "Non-data", ZSTR_VAL(ce->name),
parent_ce->ce_flags & ZEND_ACC_DATA_CLASS ? "data" : "non-data", ZSTR_VAL(parent_ce->name)
);
}

if (ce->parent_name) {
zend_string_release_ex(ce->parent_name, 0);
}
Expand Down
Loading
Loading