10

I've never seen this structure anywhere, so I wonder if there's something wrong with an expression like this:

if (condition) {

    use Symfony\Component\HttpFoundation\Response;

}
5

2 Answers 2

13

The only thing use does is to alias a class name. That's it. Nothing more.
Instead of having to repeatedly write the fully qualified classname in your script:

$q = new \Foo\Bar\Baz\Quux;
if ($q instanceof \Foo\Bar\Baz\Quux) ...

You can shorten that to:

use Foo\Bar\Baz\Quux;

$q = new Quux;
if ($q instanceof Quux) ...

As such, it makes absolutely no sense to want to use use conditionally. It's just a syntactic helper; if it could be used conditionally your script syntax would become ambiguous, which is something nobody wants.

It doesn't reduce code loading, because code is only loaded explicitly by require/include calls or via autoloading. The latter one is greatly preferred, since it already lazily springs into action only when needed.

4
  • There is sense in this. Let's say I am switching vendors for a backend API. I want to be able to switch from using one client to another without writing a ton of code to make the swtich. Let's say I define the API vendor in an environment variable: VENDOR=SignalWire or VENDOR=Twilio In the class that sets up the client connection, I want to initialize the 'correct client'. A conditional use that loads the right vendor's code would be perfect and then I don't have to conditionally use classes in the rest of the code which might have dozens of 'new's.
    – Rick Fisk
    Commented Jun 16, 2021 at 20:16
  • 2
    The switch shouldn’t happen at the level of switching out the class name! One piece of your code should be clearly aware of the two different class names, and then consciously instantiate one, and then you can pass that instance of one class or the other around. That’s proper OOP polymorphism.
    – deceze
    Commented Jun 17, 2021 at 4:40
  • yes, you're right. The way I ended up solving this was to use ClassA as Foo; Use ClassB as Bar; and then conditionally return the properly instantiated class depending on the environment.
    – Rick Fisk
    Commented Jun 18, 2021 at 7:24
  • 1
    Yes. A further reason why that is the correct way to go is that the constructors may be very different. Even if the classes' interfaces are identical and interchangeable, if they need different constructor arguments, something in your code needs to be clearly aware of which class is which, and you can't let PHP make that switcheroo at the syntactic level.
    – deceze
    Commented Jun 18, 2021 at 7:31
9

This will throw a syntax error. From TFM:

The use keyword must be declared in the outermost scope of a file (the global scope) or inside namespace declarations. This is because the importing is done at compile time and not runtime, so it cannot be block scoped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.