Does PHP read functions before they are called?
Asked Answered
C

2

10

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?

Chatelain answered 20/7, 2016 at 7:34 Comment(4)
If you want more information about PHP execution this may interest you.Relative
Every language has to process the source files for grammar/syntax. So yes, PHP will do so too. But if you don't invoke the functions, nothing really happens. Parsing and syntax checking is extremely quick. If you're after some silly optimization move - just don't do it, this won't help you.All
What is slow for a computer is often fast enough for us - A smart dev You can be sure; php does a lot of unnecessary work, without us noting it. You should always try to code as clean and clear as possible in terms of software architecture, which most of the time does not mean the fastest way for a computer. Optimization is what you do if everything works as it should.Stucker
I've noticed inflated memory for say a web request, when you include large libraries that you may not use. Auto-loading and opcode caching could help here. File system scans and access can slow your app. A one file app, may be more performant, even if it does guzzle more memory.Brianbriana
B
11

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.

Bainbrudge answered 20/7, 2016 at 7:42 Comment(1)
It's very important to note that if you use the opcache extension (as any professional PHP deployment does), then files are only compiled once instead of on every execution. As such there is no overhead from compiling functions. The only overhead is binding them at runtime. This is not a problem for functions, but may be one for classes (if you have 1000s of unused classes bound at runtime.)Moazami
M
1

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.

Mastigophoran answered 20/7, 2016 at 8:9 Comment(4)
Autoloading means extra filesystem operations. I could argue that it slows your system down. It is always a good idea to use autoloading nonetheless, but in the scope of the question I would not agree with you.Stucker
@Stucker - surely in the scope of the question, it would depend on just how much of the code is going to be loaded? If we only use one or two of the hundred classes each time (which is the implication I got from the question), then autoloading will definitely still be a saving compared with having the whole lot loaded every time.Mastigophoran
@Stucker If you use a properly configured opcache, then autoloading does not cause extra filesystem operations.Moazami
@Moazami yeah sure, and if buy a stronger server it doesn't matter at all... But that's not helping here, does it? We're merely discussing the bare basics of what the parser does with classes/files, and without relatively complex improvements to your server stack like using autoloaders arguably won't help you.Stucker

© 2022 - 2024 — McMap. All rights reserved.