1

When using namespaces in PHP, you have a choice for how to refer to them. You can put them at the top of your class

use Symfony\Component\HttpFoundation\JsonResponse;

Or you can include them as you call the class:

return Symfony\Component\HttpFoundation\JsonResponse::create($data);

My question is, are there any performance considerations which would favor one method over the other?

I could imagine, based on no real knowledge whatsoever, that it's possible that declaring use in a class might sort of pre-load class definitions that never wind up getting used in the actual code execution, depending on the data-- which would be addressed by only referencing the namespaced class when you're actually calling it. Or, if you make a declaration during development, but never end up calling that class, but don't think to delete it when you're wrapping up, your code would always loading that class definition, and never using it.

Continuing to use my very spotty knowledge of PHP at runtime, it's possible that declaring namespaces at the class definition gets compiled, making it run faster when you actually call that class, instead of having to make some sort of 'dynamic' lookup when calling the class.

Or are useing namespaces at the top of class declarations only there for the developer's convenience, to avoid a lot of repetitive typing?

I found this question, Are namespaces bad for performance?, but it is only asking if using namespaces at all, anywhere in the code, have a negative impact on performance. Also it's 7 years old; it's possible the information is outdated.

user151841
  • 17,377
  • 29
  • 109
  • 171

1 Answers1

-1

I would say both, for example in the script:

use my\php\namesapce\TestClass;

echo TestClass::class;
$instance = new TestClass();
  1. It is good for the performance because:
    • PHP just needs to read/evaluate a single word (namely TestClass) each time.
    • PHP internally stores the name (TestClass) as key for the full-path in a key-value pair temporarily (as long as the file is being read).
    • The file takes less space and hence less time to be loaded in the first place.
  2. It is more convenient because the developer does not need to write the whole path each time.

And it does not affect the way that the auto-loader works (see another related post) because:

PHP will call the autoloaders exactly the same way with exactly the same parameters, with or without the use-statement.

Top-Master
  • 7,611
  • 5
  • 39
  • 71
  • 1
    Not sure about the performance claims - although there may be some very minor differences, they would most likely be unmeasurable. For example - with a block size of 4KB, not sure how much difference not having to type the full name each time would make. – Nigel Ren Oct 13 '19 at 06:41
  • Really tiny performance improvement but still better than making it slower ;-) – Top-Master Oct 13 '19 at 06:54
  • I would like to see some data, or official statements, about performance impacts – user151841 Oct 13 '19 at 15:12