Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

php - this keyword inside slim framework closure

recently i started to learn API in PHP, i start to learning basic stuffs related to API then i decided to learn a framework and i choose slim microframework so i was reading the Dependency Container documentation and i saw this example

$app->get('/foo', function ($req, $res, $args) {
$myService = $this->get('myService');

return $res;

});

here this keyword refers to Container object so i want to know how it is possible to refer an object with this keyword in PHP closure?

question from:https://stackoverflow.com/questions/65933772/this-keyword-inside-slim-framework-closure

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It seems that the $this is the $app in this context. And from the earlier part of the documentation you can see that you create the container, add it to the app. So the container is there, injected.

$container = new SlimContainer;
$app = new SlimApp($container);

Although Slim is a microframework - it's not easy. You use Slim 3, no problem with that. If you want to understand better how the things could be structured - I recommend this tutorial (it's for Slim 4).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...