Can a PHP application have too many functions? Does the execution of a large number of PHP functions hog memory and resources? My WordPress theme in-development has a lot of functions (probably more than 100 by the time I'm done) and I'm concerned that I might have too many.

+3  A: 

Even if many functions led to more memory consumption, I'd advise you to use them anyway. A clear structure is much more important than performance.

Performance can be improved by throwing hardware at it, a badly structure application gets quickly very hard to maintain.

By the way: 100 functions are nothing!

+1 Agreed completely
OM The Eternity
+2  A: 

No, it can't. Actually, it is good to have many functions, because that means that you've divided your code into more manageable pieces. A good rule is to keep your functions small, have them do just one thing. Oh, and remember to name your functions so that the function name describes well what the function does.

Having a lots of methods will produce a small overhead in the execution time, but it is so insignificant compared with the benefits you get from having structured code, so I wouldn't worry about it.

Kim L
+4  A: 

100 functions is nothing to worry about. If your code is slow you should profile it to explicitly find the slow parts.

Remember, premature optimizations are evil

`Remember, premature optimizations are evil` mmh.. somewhat disagree ;)A good planning and design is itself an half of the total optimization
@DanielTrying to limit the number of functions because one guesses it could be a performance problem is quite different from good planning. The performance bottleneck is not always obvious, sometimes it is in quite unexpected places.
"Premature optimization is the root of all evil" - Donald Knuth."Belated pessimization is the leaf of no good" - Len Lattanzi.
Totally agree with Fabian's comment
+1  A: 

Just make your code to do it's job. Don't care about your function count.

Ahmet Kakıcı

I think yes a project can have too many functions. I don't know how you have setup your project but it sounds like you have function libraries. You may want to consider Object Oriented Development in the future. This allows you to encapsulate functionality into Objects. Therefore the object contains the functions and they are not visible to other objects (unless you want them to be). This helps to keep the API pollution down a lot.

For you memory concerns - I used to worry about this too, DON'T. Good programming practices are more important that speed. You will not even notice the difference on a Web Server anyway. Always favor maintainability over speed!

+1  A: 

I wouldn't be concerned about 100 functions. That's very few functions.

Global functions and methods are compiled, stored in memory, and indexed in a hash table (save for cached lookups implemented recently). You won't have performance deterioration when calling the functions as the number of functions grows, since accessing the hash table is done, on average, in constant time.

However, there will be more overhead parsing the scripts and, if you actually call all those functions, compiling them. This is not really a problem if the you use an opcode cache. There will also be more memory overhead, but typically, memory is not a problem in enterprise grade web servers, where it's more appropriate to try to serve the requests as fast as possible, without caring that much about the memory.

There are also stylistic issues. If you have too many global functions, consider whether:

  • You are duplicating code between those functions. Consider refactoring, moving the common code to other function and generalizing the behavior of the functions by adding parameters, where appropriate.
  • You would be better grouping functions that operate on the same data in an class.

In the end, worry about this only if you profile your application and find function calls to be a CPU bottleneck and function definitions to be a memory bottleneck.


PHP has over 3000 functions in its core, so don't worry about adding too much.