Sunday 15 August 2010

php - Optional namespaces with "use" keyword -


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

I am using Laravel 4 in my application. I have a controller and function inside. I direct specific routes for specific tasks.

However, in this controller one function needs to use external class. I use the keyword "use" in this way:

  PEM request \\\ CaptureRequest; Use PEM \ AuthorizeNet \ Aim \ PaymentFactory;   

This will work in the beginning of the controller, before opening the class, however, I was wondering if I can only activate these specific classes in this particular function for which these sections the wanted? I tried to do this, but apparently threw an invalid syntax error.

I would be happy to have this theory on this issue and behind it. I may also be asking a stupid question :) [Because I know that I can refer to the functions of these classes by using the complete reference such as Payum \ Request \ CaptureRequest \ Function . Or I can just write it at the beginning of the controller file because it will not cause any loading problem for execution.]

Example of what I want to achieve:

  // Use PEM \ request \ CaptureRequest; // Use PEM \ AuthorizeNet \ Aim \ PaymentFactory; ////// Instead of using context ... class MotawordController extends BaseController {function first () {...} function second () {Use Payum \ Request \ CaptureRequest; Use PEM \ AuthorizeNet \ Aim \ PaymentFactory; ...}    

No, you can not: < Blockquote>

Use keywords should be declared within the outermost scope (global scope) or namespace declarations of a file. The reason for this is that the timing of the import is compiled and there is no runtime, so it can not be blocked.

No comments:

Post a Comment