Do unused use statements decrease performance?

4.1k views Asked by At

I want to know if unused use statements in my class affect performance of my php website?

Does php include all classes in beginning or when need it? If second choice then I think it doesn't affect performance of my system.

For Example: Use statement 'DbConnector' is not used

use model\adapter\DbConnector;
2

There are 2 answers

4
RandomSeed On BEST ANSWER

No, the use statement does not provoke the the class be loaded (it does not even trigger an autoloader).

It just declares a short name for a class. I assume the cost in terms of CPU and RAM is in the order of a few CPU cycles and a few bytes.

0
Pelmered On

Newer versions of PHP, PHP 7 and especially PHP 7.2, are very good at optimizing code when it's complied into byte code. Unsed use statements are just stripped away by the compiler and will not even execute. Therefore it should not have any impact whatsoever. The compiler might use a few more CPU cycles while parsing the file but if you use OPCache there will be no effect on performance. The file will only be loaded when they are needed.