Advertisement
Guest User

Untitled

a guest
Nov 28th, 2021
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.59 KB | None | 0 0
  1. <?php
  2. interface Comparable {
  3.     function compareTo($other): int;
  4. }
  5.  
  6. class BigInteger implements Comparable {
  7.     public function __construct(private int $value) {}
  8.    
  9.     public function compareTo($other): int {
  10.         $type = gettype($other);
  11.        
  12.         switch ($type) {
  13.             case integer::class:
  14.                 return $this->compareToInt($other);
  15.             default:
  16.                 throw new \LogicException('Cannot compare \''.BigInteger::class.'\' with \''.$type.'\'');
  17.         }
  18.     }
  19.    
  20.     protected function compareToInt(int $other): int {
  21.         return $this->value - $other;
  22.     }
  23. }
  24.  
  25. $a = new BigInteger(15);
  26. var_dump($a->compareTo(35));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement