SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    best practices in number of files in app

    What is everyone's thoughts on the number of php files in your web application? Do you keep many files with few operations located in them, or few files with many operations in them? Is there an advantage to one over the other in terms of performance, upgrade ability, etc?

  2. #2
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Generally, the more files you load the longer the script runs so having as few files as possible is best from performance point of view.

    However, not splitting parts of your code into many files in a large application would be very messy and hard to maintain so it doesn't make sense to do that. File access is not a huge performance hit so in most cases there's no need to worry and until it becomes a problem I wouldn't try to pack everything into one file. Moreover, opcode caches like APC, PHP Accellerator, etc. can speed up includes a lot by caching them in memory.

    It's good to keep each class in a separate file. It makes navigation around your code convenient and you can easily set up class autoloading to avoid unnecessary includes every time you use a class.

  3. #3
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Yeah.

    I keep all my classes in separate files, and the same goes for interfaces too. My classes are also generally quite small and focused on just doing one thing. I rarely have files with more than a few hundred lines in them. It works well and keeps the code organised.

    *Edit: I also do MVC for practically everything if I can help it, and tend to keep my controllers slim, my models simple and my views dumb (very little logic in my views). If I need to do a complex process involving a bunch of models, I tend to create a class to encapsulate the process itself, so I don't have any complicated logic in my models. My models tend to just have access and setter methods in them, and little logic other than checking for dependencies and things like that.

    I also almost never echo html from my classes or from php in general. One of my biggest pet hates is to see people echoing html directly from php, especially within a class or function context.

    Quote Originally Posted by Lemon Juice View Post
    Generally, the more files you load the longer the script runs so having as few files as possible is best from performance point of view.

    However, not splitting parts of your code into many files in a large application would be very messy and hard to maintain so it doesn't make sense to do that. File access is not a huge performance hit so in most cases there's no need to worry and until it becomes a problem I wouldn't try to pack everything into one file. Moreover, opcode caches like APC, PHP Accellerator, etc. can speed up includes a lot by caching them in memory.

    It's good to keep each class in a separate file. It makes navigation around your code convenient and you can easily set up class autoloading to avoid unnecessary includes every time you use a class.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •