Functional Programming in PHP: Part I

Functional-php
$add = function (int $x, int $y) : int {
return $x + $y;
} //takes two integer inputs, x and y
$add = function (int $x) use ($y) : int {
return $x + $y;
} //takes an external value, $y and adds it to $x
class simpleArithmetic
{
private $valX;

private $valY;

public function __construct(int $valX, int $valY)
{
$this->valX = $valX;
$this->valY = $valY;
}
public function add() : int
{
return $this->valX + $this->valY;
}
}
$add = (new SimpleArithmetic(12, 13))->add(); //returns 25
//function as argument
$stringManip = function (callable $fn, string $str) {
return $fn($str);
}
$stringManip("strtoupper", 'foo'); //should return FOO//function as returned result
$stringManipTwo = function ($str) {
return function ($fn) use ($str) {
return $fn($str);
};
}
$stringManipTwo('foo')('strtoupper'); //should return FOO as well

I write PHP and I know things — kind of. https://chemem.site

I write PHP and I know things — kind of. https://chemem.site