Should I use Custom helpers in Laravel instead of simple static methods of a class?

Multi tool use
Multi tool use


Should I use Custom helpers in Laravel instead of simple static methods of a class?



Helpers:



We add arrayhelper.php to AppHelpers folder
Then we add


"files": [
"app/helpers/arrayhelper.php",
]



to section


"autoload": {
}



in composer.json.



Now it's possible to define methods and use them globally.



Static methods:



make class ArrayHelper
define static method (e.g. doSmthg())
and then it's possible to call it like this ArrayHelper.doSmthg()


doSmthg()


ArrayHelper.doSmthg()



Question:



if there is some benefits (e.g. better performance, less memory usage) of using helpers in Laravel or it's just matter of habit?



When to choose between helpers and static methods?



P.S. There was some similar questions, but I read through all answers and I didn't find what I exactly wanted to know.





In my own opinion it is always better to use functionality that already exists then creating my own. So I would say that it is better to create helpers for those few things that need a helper. But I also think that if you need a lot of them the laravel helper files get a bit crowded.
– Marcus
Jul 2 at 11:32






@Marcus that's true. If is slows the framework, probably not if not overused. It is only beneficial if certain functions are of common need. For instance, if it encompasses several tables or if it is table (model) independent. I use custom functions. Albeit very few. All helper functions are in-build Laravel functions. So yes. They are very common by design. Just don't overdo it.
– Dimitri Mostrey
Jul 2 at 12:32





1 Answer
1



Try this solution:


app/Http/arrayhelper.php



add a function like this:


if(!function_exists('test_function_name')){
function test_function_name(){

return 'This is my return';

}

}



Add this line to your comopser.json :


"autoload": {
...
"files":[
"app/Http/arrayhelper.php"
]
},



run this command line comopser update


comopser update



Then from each file in your project you can call your function like that:


test_function_name()





You didn't answer to my question, I know how to add helper
– Aleksandrs
Jul 2 at 11:40






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

jiy,3CRzSt2qW7,EKN 4dJXqD,5lVo,A28UZIxqbSJAxGewPT 8JX,uVGMNd6gqNZ waOOp4pp lZYsjOk
rNUJ9ZRvW,0PslDF34aJQRbVXW

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications