Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will splitting up included functions improve PHP performance?

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 :)

like image 780
user52005 Avatar asked Dec 22 '22 12:12

user52005


2 Answers

  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).

like image 183
Assaf Lavie Avatar answered Feb 24 '23 10:02

Assaf Lavie


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.

like image 29
Carsten Avatar answered Feb 24 '23 09:02

Carsten