Created
November 24, 2014 23:08
-
-
Save requinix/1375e605838e67dde10f to your computer and use it in GitHub Desktop.
Why reverse-inheritance of namespaces would suck (part 2)
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 | |
// https://bugs.php.net/bug.php?id=68485 | |
namespace Math { | |
class Calculator { | |
public static function calculate($operator, $operand1, $operand2) { | |
switch ($operator) { | |
case "+": | |
return \Math\AddOperator::add($operand1, $operand2); | |
case "-": | |
return \Math\AddOperator::subtract($operand1, $operand2); | |
case "*": | |
return \Math\MultiplyOperator::multiply($operand1, $operand2); | |
case "/": | |
return \Math\MultiplyOperator::divide($operand1, $operand2); | |
default: | |
throw new \Exception("Invalid operator '{$operator}'"); | |
} | |
} | |
} | |
class AddOperator { | |
public static function add($op1, $op2) { return $op1 + $op2; } | |
public static function subtract($op1, $op2) { return $op1 - $op2; } | |
} | |
class MultiplyOperator { | |
public static function multiply($op1, $op2) { return $op1 * $op2; } | |
public static function divide($op1, $op2) { return $op1 / $op2; } | |
} | |
} | |
namespace MoreMath { | |
class BetterCalculator extends \Math\Calculator { | |
public static function calculate($operator, $operand1, $operand2) { | |
switch ($operator) { | |
case "div": | |
return \MoreMath\MoreOperators::div($operand1, $operand2); | |
case ">>": | |
return \MoreMath\MoreOperators::rshift($operand1, $operand2); | |
case "<<": | |
return \MoreMath\MoreOperators::lshift($operand1, $operand2); | |
default: | |
return parent::calculate($operator, $operand1, $operand2); | |
} | |
} | |
} | |
class MoreOperators { | |
public static function div($op1, $op2) { return floor($op1 / $op2); } | |
public static function rshift($op1, $op2) { return $op1 >> $op2; } | |
public static function lshift($op1, $op2) { return $op1 << $op2; } | |
} | |
} | |
namespace { | |
echo MoreMath\BetterCalculator::calculate("+", 1, 2); // 3 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment