0

Starter: I was wondering if we could use "use" keyword as optional in our applications.

I am using Laravel 4 in my application. I have a controller and functions inside it. I direct specific routes to specific functions.

However, one function in this controller needs to use an outside class. I use "use" keyword like this:

use Payum\Request\CaptureRequest;
use Payum\AuthorizeNet\Aim\PaymentFactory;

This will work at the beginning of the controller, before Class opening. However, I was wondering if I could activate these use clauses only in this specific function which requires these kind of classes? I tried it, but it apparently threw an invalid syntax error.

I would be glad if I could have answer on this issue and theory behind this. I may even be asking a stupid question :) [Because I know I can reference to the functions in these classes by using full reference like Payum\Request\CaptureRequest\Function. Or I can just write this at the beginning of the Controller file as it would not give any loading problem to the execution.]

Example of what I want to achieve:

//use Payum\Request\CaptureRequest;
//use Payum\AuthorizeNet\Aim\PaymentFactory;
////// instead of using the reference here...

class MotawordController extends BaseController {

    function first()
    {
       ...
    }

    function second()
    {

        use Payum\Request\CaptureRequest;
        use Payum\AuthorizeNet\Aim\PaymentFactory;

        ...
    }
Sunny Patel
  • 7,830
  • 2
  • 31
  • 46
Oytun
  • 474
  • 5
  • 15

1 Answers1

1

No you can´t. From PHP manual:

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.

hynner
  • 1,352
  • 1
  • 11
  • 20