madorin/matex

Examples difficult to understand

tomotom opened this issue · 4 comments

This is only a suggestion, but I needed time to understand examples. I suggest you to remove the object requirement. For function example :

function sum() {
$arguments = func_get_args();
$result = 0;
foreach ($arguments as $argument)
$result += $argument;
return $result;
}
$evaluator = new \Matex\Evaluator();
$evaluator->functions = [
'sum' => ['ref' => 'sum', 'arc' => null]
];
echo $evaluator->execute('sum(1, 2, 3)');

Thanks @tomotom , i'll check more that example.
The intention was to provide a sample with dynamic arguments count, as "sum" is already defined into php core.

Hi Madorin, This threw me a bit also. I was trying to call generic php math functions, like max() with your syntax. PHP Math functions: http://php.net/manual/en/ref.math.php

I finally got it to work, but I wasn't sure about the Object or what your 'ref' and 'arc' keys were until I studied your script a bit more. So a suggestion would be 1.) an explanation of how function arrays work and are set. 2.) Including PHP's arithmetic functions by default. Thank you for a great script! It is very handy and concise! I plan to study it in more detail and learn from it.

@nuntius-rex, thanks for suggestions, i'll try to document then better example and source code.
The idea was to not overload engine itself by adding functions that you are not going to use.
We had projects where we need to do just sum and some our custom, no sin, cos etc...
About parameters:
ref - function name [including namespace, class | object etc..]
arc - argument count, if null - any number, unknown
Theoretically all math functions can be included automatically by having a special PHP file that add math function to Evaluator class.

Thanks @madorin I can appreciate the desire to keep things concise, clean and minimal. It's definitely best practice. I am also just adding what I need. Thank you for the clarification on the parameters.