public function testError() { $math = new Math(); // no input try { $math->add(); } catch (\Exception $e) { $this->assertEquals('Please provide numbers to add', $e->getMessage()); } // no input try { $math->subtract(); } catch (\Exception $e) { $this->assertEquals('Please provide numbers to subtract', $e->getMessage()); } // no input try { $math->multiply(); } catch (\Exception $e) { $this->assertEquals('Please provide numbers to multiply', $e->getMessage()); } // no input try { $math->divide(); } catch (\Exception $e) { $this->assertEquals('Please provide numbers to divide', $e->getMessage()); } //invalid input try { $math->setA('adf'); } catch (\Exception $e) { $this->assertEquals('Invalid input', $e->getMessage()); } }
/** * @requires extension bcmath * @runInSeparateProcess */ public function testBcMath() { if (!extension_loaded('bcmath')) { $this->markTestSkipped('The Bcmath extension is NOT loaded! You must enable it to run this test'); } elseif (extension_loaded('gmp')) { $this->markTestSkipped('The GMP extension is loaded! You must remove it to run this test'); } Math::add("3324234234234234234", "3324234234234234234"); $this->assertEquals(new BcEngine(), Math::getEngine()); }
{ $this->age += 1; } public function age() { return $this->age; } } $joe = new Person(); $joe->haveBirthday(); $joe->haveBirthday(); echo ' joe ', $joe->age(), PHP_EOL; $jane = new Person(); $jane->haveBirthday(); echo ' jane ', $jane->age(), PHP_EOL; // '::' is the Scope Resolution Operator //echo Person::$age; class Math { //When you think 'static', think 'share; public static function add() { return array_sum(func_get_args()); } } //$math = new Math; // //var_dump($math->add(1, 2, 3, 4, 5, 6, 7)); //This code is replaced with echo ' math ', Math::add(1, 2, 3), PHP_EOL;
<?php /** * Created by PhpStorm. * User: roger * Date: 4/10/15 * Time: 17:46 */ class Math { public function add() { return array_sum(func_get_args()); } } $math = new Math(); var_dump($math->add(1, 2, 3));
<?php class Math { public static function add(...$nums) { return array_sum($nums); } } echo Math::add(1, 2, 3);
var_dump($f, $f2, $a); // test map $arr = array('fdjiefwf', '123'); Funt::map($arr, function ($k, $v) { echo "{$k} -> {$v} \n"; }); Funt::map($arr, lambda('$k, $v -> print "$k ==> $v \\n"')); $str = "abcdefg"; Funt::map($str, function ($ch) { echo "{$ch} \n"; }); $arr = array(1, 2, 3, 4, 5); echo Funt::reduce(function ($r, $k, $v) { return $r + $v; }, 1, $arr) . "\n"; echo Math::add(1, 2, 3, 4, 5, 6) . "\n"; echo Math::mul(1, 2, 3, 4, 5, 6) . "\n"; echo Math::pow(2, 3, 2) . "\n"; echo 2 ** 3 ** 2 . "\n"; function abc() { } var_dump('abc') . "\n"; // PHP不支持“惰性特征” // $arr = array(1/0, 3, 4); // var_dump(count($arr)); // PHP的闭包“惰性特征” $arr = function () { return array(1 / 0); }; // 使用PHP的yeild协同机制,可以实现类似nodejs的事件机制。
/** * Returns the sum of this number and the given one. * * The result has a scale of `max($this->scale, $that->scale)`. * * @param \Arki\Math\Number|int|float|string $that The number to add. Must be convertible to a BigDecimal. * * @return BigDecimal The result. * * @throws \ArithmeticError If the number is not valid, or is not convertible to a BigDecimal. */ public function plus($that) { $that = self::of($that); if ($that->intVal->isZero() && $that->scale <= $this->scale) { return $this; } $this->scaleValues($this, $that, $a, $b); $value = Math::add($a, $b); $scale = $this->scale > $that->scale ? $this->scale : $that->scale; return new self(BigInteger::of($value), $scale); }
<?php function autoloader($class) { include 'classes/' . $class . '.php'; } spl_autoload_register('autoloader'); $math = new Math(); echo $math->add(2, 3);
public function testAdd() { $math = new Math(); $this->assertEquals(3, $math->add(1, 2)); $this->assertEquals(10, $math->add(3, 7)); }
<?php //most of the time statics are not the right choice //static something if you need to have it shared across all similar classes //public statics are namespaced but globally accessible functions class Math { //add() doesn't need to be dynamic //makes it a global function without requiring a specific instance public static function add(...$nums) { //have to be careful to not call other functions since this is static return array_sum($nums); } } echo Math::add(1, 2, 3, 4);