Skip to content

Instantly share code, notes, and snippets.

@jbtronics
Last active July 28, 2021 14:42
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jbtronics/84ab9a88ded5808f61f0c2537794f1fd to your computer and use it in GitHub Desktop.
[RFC] PHP Userspace Operator Overloading Demo
<?php
/*
* Demo applicaton to demonstrate userspace operator overloading.
*/
class Vector3 {
public float $x;
public float $y;
public float $z;
public function __construct(float $x, float $y, float $z) {
$this->x = $x;
$this->y = $y;
$this->z = $z;
}
public function abs()
{
return sqrt($this->x**2 + $this->y**2 + $this->z**2);
}
public function __toString()
{
return "({$this->x}, {$this->y}, {$this->z})";
}
public static function __add($lhs, $rhs): ?Vector3
{
if(!$lhs instanceof Vector3 || !$rhs instanceof Vector3) {
return PHP_OPERAND_TYPES_NOT_SUPPORTED;
}
return new static($lhs->x + $rhs->x, $lhs->y + $rhs->y, $lhs->z + $rhs->z);
}
public static function __sub($lhs, $rhs): ?Vector3
{
if(!$lhs instanceof Vector3 || !$rhs instanceof Vector3) {
return PHP_OPERAND_TYPES_NOT_SUPPORTED;
}
return new static($lhs->x - $rhs->x, $lhs->y - $rhs->y, $lhs->z - $rhs->z);
}
public static function __mul($lhs, $rhs): ?Vector3
{
//Multiplication with a scalar
if (is_numeric($lhs)) {
return new static($lhs * $rhs->x, $lhs * $rhs->y, $lhs * $rhs->z);
}
if (is_numeric($rhs)) {
return new static($rhs * $lhs->x, $rhs * $lhs->y, $rhs * $lhs->z);
}
//Cross product
if($lhs instanceof static && $rhs instanceof static) {
return new static(
$lhs->y * $rhs->z - $lhs->z * $rhs->y,
$lhs->z * $rhs->x - $lhs->x * $rhs->z,
$lhs->x * $rhs->y - $lhs->y * $rhs->x);
}
return PHP_OPERAND_TYPES_NOT_SUPPORTED;
}
public static function __concat($lhs, $rhs): ?float
{
if(!$lhs instanceof Vector3 || !$rhs instanceof Vector3) {
return PHP_OPERAND_TYPES_NOT_SUPPORTED;
}
return $lhs->x * $rhs->x + $lhs->y * $rhs->y + $lhs->z * $rhs->z;
}
}
//Vector definition
$a = new Vector3(1, 2, 3);
$b = new Vector3(3, 2, 1);
printf("a: %s\n", $a);
printf("b: %s\n\n", $b);
printf("Addition: %s\n", $a + $b);
printf("Subtraction: %s\n", $a - $b);
printf("Complex operation %s\n", ($a+$b)*2*($a-$b));
printf("Minus a:%s\n", -$a);
printf("Multiplication with scalar: %s\n", 2 * $a);
printf("Multiplication with scalar: %s\n", $a * 2);
printf("Cross product: %s\n", $a * $b);
printf("Dot product: %s\n", $a . $b);
/*************************************************************************************
* Output:
*
* a: (1, 2, 3)
* b: (3, 2, 1)
*
* Addition: (4, 4, 4)
* Subtraction: (-2, 0, 2)
* Complex operation (16, -32, 16)
* Minus a:(-1, -2, -3)
* Multiplication with scalar: (2, 4, 6)
* Multiplication with scalar: (2, 4, 6)
* Cross product: (-4, 8, -4)
* Dot product: 10
*
****************************************************************************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment