gpt4 book ai didi

php - 带有 "use"关键字的可选命名空间

转载 作者:行者123 更新时间:2023-12-04 17:04:26 25 4
gpt4 key购买 nike

初学者:我想知道我们是否可以在我们的应用程序中使用“use”关键字作为可选。

我在我的应用程序中使用 Laravel 4。我有一个 Controller 和里面的功能。我将特定路由指向特定功能。

但是,此 Controller 中的一个函数需要使用外部类。我像这样使用“use”关键字:

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

这将在 Controller 的开头,在类打开之前起作用。但是,我想知道是否只能在需要这些类的特定函数中激活这些 use 子句?我试过了,但它显然抛出了一个无效的语法错误。

如果我能对这个问题和背后的理论有答案,我会很高兴。我什至可能会问一个愚蠢的问题:)
[因为我知道我可以通过使用像 Payum\Request\CaptureRequest\Function 这样的完整引用来引用这些类中的函数。 .或者我可以在 Controller 文件的开头写这个,因为它不会给执行带来任何加载问题。]

我想要实现的示例:
//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;

...
}

最佳答案

不,你不能。来自 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.

关于php - 带有 "use"关键字的可选命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17218685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com