Jump to content

How Do You Structure Your Functions In A File? How Many Lines Of Are Too Much ?


Recommended Posts

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.

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 by berridgeab

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 by Andy123
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.