Created
September 30, 2014 14:01
-
-
Save seeruk/bfd270b09da4b204b293 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 | |
/** | |
* Sum Interface | |
* | |
* @author Elliot Wright <elliot@byng.co> | |
*/ | |
interface SumInterface | |
{ | |
/** | |
* Sum a list of numbers | |
* | |
* @param array $numbers | |
* @param number $total | |
* | |
* @return number | |
*/ | |
public static function sum(array $numbers, $total = 0); | |
} | |
/** | |
* Recursive Summing Class | |
* | |
* @author Elliot Wright <elliot@byng.co> | |
*/ | |
class RecursiveSum implements SumInterface | |
{ | |
/** | |
* {@inheritDoc} | |
*/ | |
public static function sum(array $numbers, $total = 0) | |
{ | |
$total += array_pop($numbers); | |
if (count($numbers) > 0) { | |
return self::sum($numbers, $total); | |
} else { | |
return $total; | |
} | |
} | |
} | |
$numbers = [10, 20, 30, 40, 50]; | |
$sum = RecursiveSum::sum($numbers); | |
var_dump($sum); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment