0

I'm refactoring a WordPress plugin and I have two main classes with an average of 500 lines between the two. There are other files in the plugin, but these are the ones where I keep most of the functionality.

I am refactoring in order to make a more elegant code out of this monster, and separating functionality into other objects to keep my code more cohesive and friendlier for potential contributors.

My intention is to instantiate only one object every time the plugin is used, and get the rest of the functionality out of different classes with static methods (so I won't be needing their state).

Does having many classes in different files have a relevant impact on the system's performance? Considering the situation, is it worth having static functions from auxiliary classes in different files or should I keep everything in fewer files to avoid impacting the system's performance?

2
  • My gut feeling is "more files, more abstraction, slower code" but I highly, highly doubt it would bottleneck the system before your database layer. Not posting this as an answer because its just speculation, but I'd say you're a bit premature on this specific type of optimization.
    – The Maniac
    Aug 1, 2014 at 0:52
  • This is more a generic PHP question that has been answered many times. Will it effect performance, yes, will it be noticeable, no, especially if using caching, it's not noticeable or very relevant.
    – Wyck
    Aug 1, 2014 at 2:56

1 Answer 1

2

In practice in wordpress the number of SQL queries has the biggest impact on the performance of the site and the quality of the PHP code is usually irrelevant.

In theory for any additional PHP file that you include you delay your code by the time needed to fetch the file from the disk. This time can be small if the fike are memory mapped or big if it rund on an slow old harddisk. Therefor you should try to minimize the number of PHP files that you use.

The other factor is parsing. Before executing any file the PHP interpreter has to parse it, and if the file contain unused function then the parsing time was wasted.

Therefor the best approach to organizing your code is to have the core most used parts of it in one file and load other files only when needed.

But in practice anyone that cares about the execution speed on PHP level will use PHP code caching which if set correctly will cause that all php files will be read only once and all php code will be parsed only once.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.