Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Right Now I am working on a custom MVC Application in PHP and I was confused where to include several simple common functions in my application. Functions such as truncateString() and to check if multiple strings are empty such as:

function truncateString($string, $length, $addDots = true) {
    $str = substr($string, 0, $length);
    if($addDots == true && (strlen($string) > $length)) {
        $str .= "...";
    }
    return $str;
}

This is my folder structure:

image

Should I have a seperate file or a class for functions? Or should i have it in helpers?

question from:https://stackoverflow.com/questions/65907292/place-for-common-functions-in-mvc-php

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

1 Answer

You should decide for your self what do you want to use in your project: functions, classes or both. It's up to you.

Then just group your helper functions upon file or class. Do not add all functions to one file or class. With this approach maintaining of these functions will be more easier.

For example, you can create in your helpers folder class String or string_functions.php file for string functions, class Array or array_functions.php file for functions for work with arrays and so on.

Also as examples you can check same libraries in Yii framework:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...