Skip to content

Instantly share code, notes, and snippets.

@iluuu1994
Created November 5, 2020 14:45
Show Gist options
  • Select an option

  • Save iluuu1994/5998bdcd4e0aeea7d5258096e58c940e to your computer and use it in GitHub Desktop.

Select an option

Save iluuu1994/5998bdcd4e0aeea7d5258096e58c940e to your computer and use it in GitHub Desktop.
<?php
data class Point(
int $x,
int $y,
);
// Compiles to
class Point {
public initonly int $x;
public initonly int $y:
public function __construct(int $x, string $y)
{
$this->x = $x;
$this->y = $y;
}
public static function __isIdentical(Point $lhs, Point $rhs): bool
{
return $lhs->x === $rhs->x
&& $lhs->y === $rhs->y;
}
public function __deconstruct(&$x, &$y): void
{
$x = $this->x;
$y = $this->y;
}
}
$point1 = new Point(1, 2);
$point2 = new Point(1, 2);
$point1 === $point2; // true
in_array($point1, [$point2], true); // true
[$x1, $y1] = $point1; // $x1 = 1, $y1 = 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment