Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does PHP read functions before they are called?

I declare 100 functions, but I don't actually call any of them. Will having so many functions defined affect loading time?

Does PHP process these functions before they are called?

like image 210
wordpress user Avatar asked Jul 20 '16 07:07

wordpress user


Video Answer


2 Answers

Just to be clear, even having hundreds of unused classes and functions is not going to make much difference to the performance of your program. Some difference, yes maybe, but not much. Improving the code that is being run will make a bigger difference. Don't worry about optimising for language mechanics until you've got your own code perfect. The key to performance optimisation is to tackle the biggest problems first, and the biggest problems are very rarely caused by subtle language quirks.

If you do want to minimise the effect of loading too much code that isn't going to be used, the best way to do this is to use PHP's autoloading mechanism.

This probably means you should also write your code as classes rather than stand-alone functions, but that's a good thing to do anyway.

Using an autoloader means that you can let PHP do the work of loading the code it needs when it needs it. If you don't use a particular class, then it won't be loaded, but on the other hand it will be there when you need it without you having to do an include() or anything like that.

This setup is really powerful and eliminates any worries about having too much code loaded, even if you're using a massive framework library.

Autoloading is too big a topic for me to explain in enough detail in an answer here, but there are plenty of resources on the web to teach it. Alternatively, use an existing one -- pretty much all frameworks have an autoloader system built-in, so if you're using any kind of modern PHP framework, you should be able to use theirs.

like image 131
Simba Avatar answered Oct 13 '22 00:10

Simba


Yes, php parses all functions on the run, and checks possible syntax errors , (though it does not execute them all this time) and registers their name as a symbol.
When you call any of the functions, php searches for the function in the registered symbol table for function name and then executes that function.
So, better to use functions of your purpose only as it will increase the size of symbol table.

like image 21
Shudhansh Shekhar Avatar answered Oct 13 '22 01:10

Shudhansh Shekhar