Last active
January 28, 2020 23:00
-
-
Save jbtronics/ee6431e52c161ddd006f8bb7e4f5bcd6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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) | |
{ | |
if ($lhs instanceof static && $rhs instanceof static) { | |
return new static($lhs->x + $rhs->x, $lhs->y + $rhs->y, $lhs->z + $rhs->z); | |
} | |
throw new InvalidArgumentException("Both arguments must be a Vector3!"); | |
} | |
public static function __sub($lhs, $rhs) | |
{ | |
if($lhs instanceof static && $rhs instanceof static) { | |
return new static($lhs->x - $rhs->x, $lhs->y - $rhs->y, $lhs->z - $rhs->z); | |
} | |
throw new InvalidArgumentException("Both arguments must be a Vector3!"); | |
} | |
public static function __mul($lhs, $rhs) | |
{ | |
//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); | |
} | |
throw new InvalidArgumentException("Both arguments must be a Vector3!"); | |
} | |
public static function __concat($lhs, $rhs) | |
{ | |
//Scalar product | |
if($lhs instanceof static && $rhs instanceof static) { | |
return $lhs->x * $rhs->x + $lhs->y * $rhs->y + $lhs->z * $rhs->z; | |
} | |
throw new InvalidArgumentException("Both arguments must be a Vector3!"); | |
} | |
public static function __compare($lhs, $rhs) | |
{ | |
//Compare based on absolute values | |
if($lhs instanceof static && $rhs instanceof static) { | |
return $lhs->abs() <=> $rhs->abs(); | |
} | |
throw new InvalidArgumentException("Both arguments must be a Vector3!"); | |
} | |
} | |
//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); | |
//Comparison test | |
if ($a == $b) { | |
printf("a and b has the same length!\n"); | |
} | |
if ($a !== $b) { | |
printf("a and b are not the same objects!\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment