John Davidson

php - Why PhpStorm 2019.3 is enforcing me to use static closures instead of non-static in Route::get() Laravel

0 comments
Message:


I used to code in Laravel, and I'm pretty good with it.


I've moved from VSCode to PhpStorm 2019.3, and it is enforcing me like it has intentions inside for language features, it gives me a little warning message that you should declare this closure method as static.


I know PhpStorm2019.3 fully supports PHP 7.4. So, the question is this, that should I always create closures functions as static like


Route::get('/', function () {
return view('posts', [
'posts' => Post::all()
]);
});

// without static closure
Route::get('/posts/{post}', function (Post $post) {
return view('post', [
'post' => $post
]);
});

// with static closure
Route::get('/posts/{post}', static function (Post $post) {
return view('post', [
'post' => $post
]);
});

and is it good practice or bad practice? Because I found PhpStorm the best IDE in the world for PHP and Laravel developers.



My Answer:
It is not necessary to always declare closure functions as static in Laravel Route::get() method. The warning message you are seeing in PhpStorm 2019.3 is just a suggestion and not a requirement.

You can still use non-static closure functions in Route::get() method without any issues. It is just a coding style preference and does not affect the functionality of your code.

If you prefer to use non-static closure functions, you can ignore the warning message in PhpStorm or adjust the settings to disable this specific warning.

Overall, it is up to you to decide whether to use static or non-static closure functions in your Laravel code.

Rate this post

5 of 5 based on 4081 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us