DEV Community

dewbiez
dewbiez

Posted on

PHP Package: Caller

fobber / caller

A library for calling functions, closures, classes and methods.

About

This is a library that can be used to call functions, closures(AKA: anonymous functions), classes and methods. It can be used for calling dynamic values, say a handler in an HTTP router. Or containers.

Installation

You can install this via composer, or manually download the files.

composer require fobber/caller

Caller Documentation

\Fobber\Caller\Caller::callClosure(
    \Closure $closure,
    array    $parameters = []
);
\Fobber\Caller\Caller::callFunction(
    string $function,
    array  $parameters = []
);
\Fobber\Caller\Caller::callClass(
    string $class,
    array  $parameters = []
);
\Fobber\Caller\Caller::callMethod(
    object $object,
    string $method,
    array  $parameters  = [],
    bool   $static_only = false
);

Caller Basic Usage

require_once __DIR__.'/path/to/autoload.php';

use \Fobber\Caller\{
    Validator,
    Caller
};

use \Fobber\Exceptions\{
    InvalidFunctionException,
    InvalidClassException,
    InvalidMethodException
};

$validator = new Validator;
$caller    = new Caller($validator);

Calling Closures

$value = $caller->
…

Hey! I published my first library to composer. Any PHP developers have time to read some code, you can check out my package on GitHub/Packagist.

If you read the code, it'd be nice if you'd let me know what you think.

You can install it via Composer, like so:

composer require fobber/caller

It's a library for calling things like functions, closures, classes and methods. You might be wondering why I would make something like this, but I made it so you can utilize it to make calls to dynamic values/handlers. Let's say you're building a Router library. And you want to able to separate your code into different files, like Controllers.

So you could so something similar to:

$object = $caller->callClass(IndexController::class);
$method = '__invoke';
$parameters = [$request, $response];
$caller->callMethod($object, $method, $parameters);

Or later once I got it implemented:

$caller->call('IndexController', [$request, $response]);

And with a neat little feature, you can have prefix and suffix parameters. If say all your controllers and their methods need a $request and $response, you can set it. And all you have to do is this:

$caller->call('IndexController');

Top comments (4)

Collapse
 
michaelgv profile image
Mike •

It looks nice, but how does it save someone time when building a router?

Typically the router logic to find and invoke would be one time per controller, the autoloader would hunt, the router will effectively create new instance (within NS or without), trigger $controller->$method([...arguments...]), typically there should be lifecycle hooks on it as well. Most of the time if you’re using a cache like Redis, you can dump the object to the redis cluster, skip autoloading and hot-restore it with deserialization, and have it available faster than regular hunting.

Collapse
 
devmazee2057282 profile image
dewbiez •

I don't know anything about lifecycle hooks. I'm not sure what you mean.

Collapse
 
coajaxial profile image
Fluttershy •

Don't try to wrap stuff that is already easily enough to use in the language itself. This applies to both your library and even its implementation (e.g. Have a look at github.com/fobber/caller/blob/mast...). But don't worry, I used to do that too :)

Collapse
 
devmazee2057282 profile image
dewbiez •

Yeah, that's actually what I noted to myself. I was already planning to remove that stuff in a future release. XD