Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is it possible to create methods with parameters in $app->register #397

Open
karamusluk opened this issue Oct 14, 2018 · 1 comment
Open

Comments

@karamusluk
Copy link

As title indicates, i am wondering whther it is possible to create a method like

`$app->register('abc', function($a, $b) {

   return $a."<br>".$b;

});`

and then use it like
echo $app->abc("hello", "xxx");

Thanks.

@nbish11
Copy link

nbish11 commented Oct 18, 2018

No, but it is really easy to do yourself.

// "MyProject/MyApp.php"

namespace MyProject;

use Klein\Exceptions\DuplicateServiceException;

class MyApp extends \Klein\App
{
    public function factory($name, $closure)
    {
        if (isset($this->services[$name])) {
            throw new DuplicateServiceException('A service is already registered under '. $name);
        }

        $this->services[$name] = $closure;
    }
}

Now use the instance of your class instead:

$myApp = new MyProject\MyApp();
$klein = new Klein\Klein(null, $myApp);


$app = $klein->app();
$app->factory('abc', function ($a, $b) {
	return $a . '<br>' . $b;
});

echo $app->abc('hello', 'xxx');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants