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

Categories

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

Place For Common Functions in MVC PHP

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

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

1 Answer

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...