Skip to content

A simple calculator app that implements math operations via a closure function.

License

Notifications You must be signed in to change notification settings

kudashevs/calculator-via-closure

Repository files navigation

Calculator via Closures

This is a case study that aims to show one of the possible ways of using closures in PHP language.

How it works

We are given a calculator() function that accepts an argument (a callable with at least one argument). To start using the closure, we need to provide an argument of the callable type with a predefined functionality (a math operation in our case) to the calculator() function, and assign the result to a variable. The function is going to enclose/remember the provided callable argument as a part of its context and is going to use it each time the closure is invoked.

$addition = calculator($opaddition);
echo $addition(1, 2); // results in 3

for more usage examples, please see the examples folder.

Notes

By default, the package uses a bootstrap that registers four global variables that correspond to the basic math operations (addition, subtraction, multiplication, division). These variables have an op- prefix in their names (i.e. $opaddition) and refer to variadic anonymous functions. For more information see the bootstrap.php file).

The validation of input arguments is implemented in the checkValidArguments function in the calculator.php file.

Note The decomposition might look strange, and it really is. However, this is just a study case on how we can use a closure to assign some predefined behavior to a variable.

Things to learn

Things that you can learn from this case study:

License

The MIT License (MIT). Please see the License file for more information.

About

A simple calculator app that implements math operations via a closure function.

Topics

Resources

License

Stars

Watchers

Forks

Languages