PHP code example of rotexsoft / callable-execution-timer

1. Go to this page and download the library: Download rotexsoft/callable-execution-timer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rotexsoft / callable-execution-timer example snippets



use \FunctionExecutionTimer\CallableExecutionTimer;

echo CallableExecutionTimer::callFunc('strtolower', 'strtolower', ['BOO']) . PHP_EOL; // outputs 'boo'

echo CallableExecutionTimer::callFunc('strtoupper', 'strtoupper', ['boo']) . PHP_EOL; // outputs 'BOO'


use \FunctionExecutionTimer\CallableExecutionTimer;

function foobar($arg, $arg2) {
    return __FUNCTION__ . " got $arg and $arg2";
}

// a function that has a by-ref argument
function mega(&$a){
    $a = 55;
    return "function mega \$a=$a";
}

echo CallableExecutionTimer::callFunc('foobar', 'foobar', ["one", "two"]) . PHP_EOL ; // outputs 'foobar got one and two'

$bar = 77;
echo CallableExecutionTimer::callFunc('mega', 'mega', [&$bar]) . PHP_EOL ; // outputs 'function mega $a=55'

// $bar now has a value of 55 after the execution of the function above


use \FunctionExecutionTimer\CallableExecutionTimer;

class foo {

    function bar($arg, $arg2) {
        return __METHOD__ . " got $arg and $arg2";
    }
}

class myclass {

    static function say_hello() {
        return "Hello!";
    }
}

// execute an instance method
echo CallableExecutionTimer::callFunc(
    'fooBar', [new foo(), "bar"], ["three", "four"]
) . PHP_EOL ; // outputs 'foo::bar got three and four'


// execute a static method
$classname = "myclass";
echo CallableExecutionTimer::callFunc(
    'myclassSay_hello', [$classname, "say_hello"]
) . PHP_EOL; // outputs 'Hello!'

// OR

echo CallableExecutionTimer::callFunc(
    'myclassSay_hello', $classname ."::say_hello"
) . PHP_EOL; // also outputs 'Hello!'



use \FunctionExecutionTimer\CallableExecutionTimer;

class A {

    public static function who() {
        return "A";
    }
}

class B extends A {

    public static function who() {
        return "B";
    }
}

// Child calling parent's implementation of method defined in both parent & child
echo CallableExecutionTimer::callFunc('B_A_who', [B::class, 'parent::who']) . PHP_EOL; // outputs 'A'

// Parent calling its own method
echo CallableExecutionTimer::callFunc('A_who', [A::class, 'who']) . PHP_EOL;  // outputs 'A'

// Child calling its own method
echo CallableExecutionTimer::callFunc('B_who', [B::class, 'who']) . PHP_EOL; // outputs 'B'


namespace Foobar {

    class Foo {

        static public function test($name) {
            return "Hello {$name}!";
        }
    }
}

namespace {
    //", ["Hannes"]
    ) . PHP_EOL; // outputs 'Hello Hannes!'

    // Syntax 2
    echo CallableExecutionTimer::callFunc(
        'FoobarFooTest', ["\\Foobar\\Foo", 'test'], ["Philip"]
    ) . PHP_EOL; // outputs 'Hello Philip!'
}


use \FunctionExecutionTimer\CallableExecutionTimer;

$func = function($arg1, $arg2) {
    return $arg1 * $arg2;
};

echo CallableExecutionTimer::callFunc('func', $func, [2, 4]) . PHP_EOL; // outputs 8

echo CallableExecutionTimer::callFunc(
    'funcInline', 
    function($arg) { return $arg; }, 
    ['in inline lambda function!']
) . PHP_EOL; // outputs 'in inline lambda function!'

// anonymous function that accepts a by-ref argument
$num = 5;
echo CallableExecutionTimer::callFunc(
    'funcInlineByRef', 
    function(int &$arg) { return "\$arg = " . ++$arg; }, 
    [&$num]
) . PHP_EOL; // outputs '$arg = 6' 

// $num now has a value of 6 at this point


use \FunctionExecutionTimer\CallableExecutionTimer;

class C {

    public function __invoke($name) {
        return "Hello {$name}";
    }
}

echo CallableExecutionTimer::callFunc('C__invoke', new C(), ['Jane!']) . PHP_EOL; // outputs 'Hello Jane!'


use \FunctionExecutionTimer\CallableExecutionTimer;

$callableObj1 = new CallableExecutionTimer('strtolowerCallback', 'strtolower');

echo $callableObj1->strtolowerCallback('BOO') . PHP_EOL; // triggers __call & outputs 'boo'
                                                         // same as $callableObj1->__call('strtolowerCallback', ['BOO'])

echo $callableObj1(['BOO']) . PHP_EOL;  // triggers __invoke & outputs 'boo'
                                        // same as $callableObj1->__invoke(['BOO'])



use \FunctionExecutionTimer\CallableExecutionTimer;

$func = function(int &$arg) { 
    return "\$arg = " . ++$arg; 
};

// Option 1 use CallableExecutionTimer::callFunc(...)
$num = -1;
echo CallableExecutionTimer::callFunc(
    'funcWithRefArg', $func, [&$num]
) . PHP_EOL; // outputs '$arg = 0' & $num will have a value of 0 after this call

// Option 2 using the __invoke(array $args) mechanism on the instance of 
// CallableExecutionTimer the callable is bound to
$num = -1;
$callableObj2 = new CallableExecutionTimer('funcWithRefArg', $func);
echo $callableObj2([&$num]) . PHP_EOL;  // triggers the __invoke(array $args) mechanism
                                        // which executes the lambda function and 
                                        // outputs '$arg = 0'.
                                        // $num will have a value of 0 after this call


///////////////////////////////////////////////////////////////////////////
// NOTE: trying to invoke the function on an instance of 
// **\FunctionExecutionTimer\CallableExecutionTimer** using the method call
// syntax that triggers **__call()** under the hood will not work, $num
// will not be passed by reference as expected and you will get a PHP
// warning to that effect.
// DON'T DO THIS
///////////////////////////////////////////////////////////////////////////
$num = -1;
$callableObj2 = new CallableExecutionTimer('funcWithRefArg', $func);
$numRef = &$num;
echo $callableObj2->funcWithRefArg($numRef) . PHP_EOL;  // Will throw a PHP Warning.
                                                        // $numRef will not be passed by
                                                        // ref because of the way 
                                                        // __call(string $methodName, array $args) 
                                                        // works, meaning that $num will still 
                                                        // have a value of -1 after the call.


    
    use \FunctionExecutionTimer\CallableExecutionTimer;

    $funcObj = new CallableExecutionTimer('strtolower', 'strtolower');

    echo $funcObj->strtolower('BOO') . PHP_EOL;
    var_export($funcObj->getLatestBenchmark());
    

    
    use \FunctionExecutionTimer\CallableExecutionTimer;

    // First clear previous benchmark info if any
    CallableExecutionTimer::clearBenchmarks(); 

    $funcObj = new CallableExecutionTimer('strtolowerMethod', 'strtolower');
    
    echo $funcObj->strtolowerMethod('BOO') . PHP_EOL;
    echo $funcObj->strtolowerMethod('ABA') . PHP_EOL;

    echo CallableExecutionTimer::callFunc(
        'funcInline', 
        function($arg) { return "Hello $arg !"; }, 
        ['Jane']
    ) . PHP_EOL;

    var_export(CallableExecutionTimer::getBenchmarks());
    

    array (
    'function' => 'strtolower',
    'args' =>
    array (
        0 => 'BOO',
    ),
    'start_time' => 81023870126000,
    'end_time' => 81023870134000,
    'total_execution_time_in_seconds' => 8.0E-6,
    'return_value' => 'boo',
    'file_called_from' => 'C:\\Code\\callable-execution-timer\\tester.php',
    'line_called_from' => 105,
    )
    

    array (
    0 =>
    array (
        'function' => 'strtolowerMethod',
        'args' =>
        array (
        0 => 'BOO',
        ),
        'start_time' => 87248086831300,
        'end_time' => 87248086840600,
        'total_execution_time_in_seconds' => 9.3E-6,
        'return_value' => 'boo',
        'file_called_from' => 'C:\\Code\\callable-execution-timer\\tester.php',
        'line_called_from' => 106,
    ),
    1 =>
    array (
        'function' => 'strtolowerMethod',
        'args' =>
        array (
        0 => 'ABA',
        ),
        'start_time' => 87248086997700,
        'end_time' => 87248087001600,
        'total_execution_time_in_seconds' => 3.9E-6,
        'return_value' => 'aba',
        'file_called_from' => 'C:\\Code\\callable-execution-timer\\tester.php',
        'line_called_from' => 108,
    ),
    2 =>
    array (
        'function' => 'funcInline',
        'args' =>
        array (
        0 => 'Jane',
        ),
        'start_time' => 87248087019400,
        'end_time' => 87248087024100,
        'total_execution_time_in_seconds' => 4.7E-6,
        'return_value' => 'Hello Jane !',
        'file_called_from' => 'C:\\Code\\callable-execution-timer\\tester.php',
        'line_called_from' => 110,
    ),
    )