views:

291

answers:

3

Hello. I have main php-file with main class. Also in this class i do require_once("func.php"); which has many useful functions for my site.

Size of func.php is very big, because there is many functions for different actions on different pages. But I include it on every page, because including called by main class. What I need to do for optimizing it?

Rewrite func.php to OOP and use in main class something like "$funcs->my_func()"? Will I won some performance? Functions which wasnt called would not occupy memory and CPU time?

Or I must rewrite func.php to many files and call each on specified page? For example: for "about.php" I will include "about_func.php" with needed functions. But it isnt comfortable I think...

Please, help me :) And sorry for my eng :)

+4  A: 
  1. How big is func.php? Are you sure the size is a problem.
  2. This seems like a performance/optimization question at heart. Are you sure that optimization is warranted? Have you measured your page's performance and proved that including this big function file is to blame for its slowness?

I suggest you answer 1 & 2 to yourself before continuing. If the motivation for your question is cleaner design and modularization, then yes, I would agree that splitting a bug "utils" file into smaller files that share a responsibility or a general area of relevance is a good idea. If, on the other hand, this is a case of premature optimization, then you'd be better off leaving poor "func.php" along (hey, sometimes it's ok to have a big common utils file, as long as it's not hurting you).

Assaf Lavie
size of including file is 151Kb.Yes, I have used NuSphere Profiler and it said me that including this file eating many time: 47% of total main.php file loading. I think this is very big time for this little operation such file including.
+3  A: 

Split it into oop classes and use the __autoload function of PHP5: http://www.php.net/manual/en/language.oop5.autoload.php

This wil load the classes only when needed and you don't have to worry about including all neccessary files. It won't give you any performance benefit but it's easier to manage smaller files for one purpose than a big one with several functions which are not dependent on each other.

Carsten
Thank you for your advice. May be I will do it.
Autoload works great. It's been my standard for including files for a long time. Only thing you need to be careful of is searching the file system. It's costly so if you do need to search instead of searching scan the directories and save the listing into an array.
gradbot
+3  A: 

Do you have a PHP accelerator enabled?

Because they keep a "compiled" version of func.php in memory which should speed things up, without any code modifications.

Although I recommend OOP, it's not because of performance reasons.

Bob Fanger
Yes! Of course, eAccelerator caches it!I have installed it on my work server, but not at home server.Thank you. eAccelerator solves my problem about including this func file.