trait \mpf\base\AdvancedMethodCallTrait

trait \mpf\base\AdvancedMethodCallTrait

Inheritance -
Implements -
Uses -
Childs -
Available since version  0.1
Source Code https://github.com/mpf-soft/mpf/tree/master/base/AdvancedMethodCallTrait.php

This trait offers 2 methods that help to call a callable function using an associated array as parameter so that the order won't matter.

This is used mainly by \mpf\web\Controller and \mpf\cli\Command to easily call actions using the parameters sent to the app when executed($_GET for web applications and console arguments for terminal applications).

Example:

class Test{
  use AdvancedMethodCallTrait;

  function toTest($a=0, $b = 'something', $c = 'nothing'){
    return "[a={$a}, b={$b} and c = {$c}]";
  }
}



$test = new Test();
echo $test->callMethod('toTest', ['c' => 'everything']);
// will output: "[a=0, b=something and c = everything]"

It does require 2 other methods to be defined in the class that uses this trait:

  • critical() to log any errors that could occur. Any \mpf\base\LogAwareObject child should be fine.
  • applyParamsToClass() - this can be used to apply some of the parameters to the class attributes also. Must be careful to not not overwrite any important attributes. This is used by \mpf\cli\Command to apply some of the arguments sent in terminal to current command and allows some general options like --debug to be available for all commands and actions. It is not recommended for websites where there is no control over what the user input will be, for that a dummy method should be created that won't apply any changes.

For details about the default uses of this trait check : \mpf\cli\Command::run() and \mpf\web\Controller::run() descriptions.

Attributes
private bool $error

Records if there was an error or not when trying to call the method.

Methods

public string[] getMethodParameters (string $name, string[] $options)

  • $name

    -no description found-

  • $options

    -no description found-

Read parameters for searched method;

public mixed callMethod (string $methodName, string[] $options)

  • $methodName

    -no description found-

  • $options

    -no description found-

Call selected method using selected options;