You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Opening this up for discussion: what do you all think about the idea of introducing class instantiation directly into the context, like $context->get(Foo::class, 'bar')? I think this could be a really cool addition, but I also realize we might be edging toward implementing a service container. Not sure if that's necessarily a bad thing—thoughts?
I have some code below, but feel free to pull my branch here for a demo.
$app = newDumbo();
class SimpleMessage
{
publicfunction__construct(
publicstring$messageOne,
publicstring$messageTwo
) {}
publicfunctiongetMessage(): string
{
return"{$this->messageOne}{$this->messageTwo}";
}
}
class InjectedMessage
{
publicfunction__construct(publicSimpleMessage$message)
{}
publicfunctiongetInjectedMessage(): string
{
return"{$this->message->getMessage()} This is an injected class.";
}
}
// Context middleware for all routes$app->use(function ($context, $next) {
$context->set(SimpleMessage::class, "Hello", "Dumbo!");
$context->set(
InjectedMessage::class,
newSimpleMessage('Hello', 'Dumbo!')
);
return$next($context);
});
$app->get("/", function ($context) {
$messageClass = $context->get(SimpleMessage::class);
$message = $messageClass->getMessage();
return$context->json([
"message" => $message,
]);
});
$app->get("/injection", function ($context) {
$injectedClass = $context->get(InjectedMessage::class);
$message = $injectedClass->getInjectedMessage();
return$context->json([
"message" => $message,
]);
});
The text was updated successfully, but these errors were encountered:
Opening this up for discussion: what do you all think about the idea of introducing class instantiation directly into the context, like
$context->get(Foo::class, 'bar')
? I think this could be a really cool addition, but I also realize we might be edging toward implementing a service container. Not sure if that's necessarily a bad thing—thoughts?I have some code below, but feel free to pull my branch here for a demo.
The text was updated successfully, but these errors were encountered: