Eritrea Posted October 7, 2012 Share Posted October 7, 2012 In my site, I have a functions.php file and I store all functions for the site ( A news site, with memberships, profiles ) So, I am still making the site, and 80% is finished, but the I feel like the file is going to slow my site down, I just checked it and I have reached 500th line on notepad++. So, I just want to know how much is too much really. What is the effect, or the best practices when putting all your functions in one file. Suppose, this for instance. There are functions that are used 10 or 20 times more than other functions. In my case, every time user browses the index page, there will be atleast 1 function that will be started. ex: ( out putting an article. ) And, there are others functions that rarely get called, like user registration, login.... So, is it ok to separate functions in different files according to their time of use? or not? I generally want to know, ( Since I am newbie ) how you deal with functions, and the best methods. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/269197-how-do-you-structure-your-functions-in-a-file-how-many-lines-of-are-too-much/ Share on other sites More sharing options...
berridgeab Posted October 7, 2012 Share Posted October 7, 2012 (edited) 500 lines is nothing. You should think about grouping your different functions into related files (i.e. file for string related functions, etc) that would be a good start as it helps maintain your code. If functions are unique to the page / file using that particular function then you may as well keep it within the same file. Honestly though your overhead will be minimal at the moment if your just beginning. Edited October 7, 2012 by berridgeab Quote Link to comment https://forums.phpfreaks.com/topic/269197-how-do-you-structure-your-functions-in-a-file-how-many-lines-of-are-too-much/#findComment-1383524 Share on other sites More sharing options...
Jessica Posted October 7, 2012 Share Posted October 7, 2012 Put related functions in one file. Then start looking into classes/objects. Quote Link to comment https://forums.phpfreaks.com/topic/269197-how-do-you-structure-your-functions-in-a-file-how-many-lines-of-are-too-much/#findComment-1383534 Share on other sites More sharing options...
Andy123 Posted October 7, 2012 Share Posted October 7, 2012 (edited) You should put related functions into the same files. Putting everything into one file will become a maintenance nightmare down the road and you will be including a lot of unnecessary code for each page request. This may not be a big deal in the beginning, but if your site will be a success, you are likely to get a whole lot more code in the future. I would suggest splitting it up into folders and files. For example, you could have a structure like "/includes/user/login.php" for functions related to logging in. I would recommend separating the functions based on what functionality they implement rather than how often they are used (because this can easily change as well). In the future once you are more familiar with PHP, you should look into OOP (Object Oriented Programming) and namespaces, and perhaps find a CMS or framework that you like. But for now, I would say to group related functions into files. Edited October 7, 2012 by Andy123 Quote Link to comment https://forums.phpfreaks.com/topic/269197-how-do-you-structure-your-functions-in-a-file-how-many-lines-of-are-too-much/#findComment-1383536 Share on other sites More sharing options...
jcbones Posted October 7, 2012 Share Posted October 7, 2012 I usually put one function per file, that is how I grab them out of my library. This causes more overhead with includes, but I can go right to the functions easily. Quote Link to comment https://forums.phpfreaks.com/topic/269197-how-do-you-structure-your-functions-in-a-file-how-many-lines-of-are-too-much/#findComment-1383542 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.