19

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;
Farid Movsumov
  • 12,350
  • 8
  • 71
  • 97

2 Answers2

36

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.

RandomSeed
  • 29,301
  • 6
  • 52
  • 87
  • There is a scenario I am fighting, where because of too many use aliases and require_once calls, the opcode cache gives up resulting in a huge performance penalty. Without use aliasing but keeping all require_once calls, it all works fine. On PHP 7.0.x, didn't test others. – oxygen Jun 06 '18 at 11:37
  • Also, this benchmark says fully qualified is always faster: https://veewee.github.io/blog/optimizing-php-performance-by-fq-function-calls/ – oxygen Jun 06 '18 at 11:40
  • 1
    Feel free to add your own aswer, however I am under the impression that you have misunderstood the point of the question. We are discussing the potential cost of *unused* `use` clauses, not the cost of *actually used* `use` clauses. – RandomSeed Jun 06 '18 at 11:43
  • I don't have an answer to make, I have a comment. Also, I'm a long time user, I guess I should've known by now! I didn't say the statements are actually used. – oxygen Jun 08 '18 at 09:37
0

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.

Pelmered
  • 2,727
  • 21
  • 22