abstract class ACurry { /** * A curry method that returns a partial call of function * or a result of its execution, depending on the number * of parameters of the invoked method * * @param array $callback * @param array $args * @return callable */ protected function curry($callback, $args = array()) { return function() use($callback, $args) { $methodInfo = new ReflectionMethod(get_class($callback[0]), $callback[1]); if (count(array_merge($args, func_get_args())) >= $methodInfo->getNumberOfParameters()) { return call_user_func_array($callback, $args); } else { return $callback[0]->curry($callback, $args); } }; } /** * Create a method $methodName by currying a method of $curryMethodName * with arguments $args * * @param string $methodName * @param string $curryMethodName * @param array $args * @return ACurry */ public function createMethod($methodName, $curryMethodName, $args = array()) { $this->$methodName = $this->curry(array($this, $curryMethodName), $args); return $this; } /** * @param string $name * @param array $args * @return mixed */ public function __call($name, $args) { if (property_exists($this, $name) && is_callable($this->$name)) { return call_user_func_array($this->$name, $args); } } }
<?php require_once 'ACurry.php'; /** * A class to calculate a mass from the density and size */ class Masses extends ACurry{ public function __construct(){ /* create method to calculate mass of iron cube */ $this->createMethod('ironCube', 'cube', array(7.8)); } /** * Method return a mass of subjection from density and size */ public function get($density, $length, $width, $height){ return $density * $length * $width * $height; } /** * Method return a mass of cube subjection from density and size */ public function cube($density, $length){ return $this->get($density, $length, $length, $length); } } $masses=new Masses(); echo $masses->ironCube(2);
Source: https://habr.com/ru/post/148899/
All Articles