繁体   English   中英

TYPO3 如何使用 '使用'

[英]TYPO3 how use 'use'

我知道,这是一些愚蠢的问题,但我有点困惑。 在此处输入图像描述

以上是来自https://docs.typo3.org/m/typo3/book-extbasefluid/main/en-us/7-Controllers/1-Creating-Controllers-and-Actions.html的截图。

如您所见,在一种情况下MyVendor之前没有“\”,而在另一种情况下是。 两种方式都可能吗?

在我的扩展中,我在Classes下有以下结构。

在此处输入图像描述

如果我用use function,就没有必要在`Classes里声明了。

use MyVendor\ExtName\Controller\MainController就足够了吗?

  1. 您正在use 'ing 命名空间,而不是文件结构,因此您必须use确切的命名空间
  2. 如果要使用 class 中的方法,则必须use class。如果 function(不是方法)在命名空间下,则必须使用 function(因为没有 class 可以使用)
  3. 前导\是可选的,但首选样式是不带前导\

// a.php
namespace MyVendor\ExtName\Controller;

function test() {
    echo 'Test2';
}

class MainController {
    public static function test() {
        echo 'Test';
    }
}

// b.php

namespace AnotherVendor\ExtName\Controller;

class MainController {
    public static function test() {
        echo 'Test 3';
    }
}

// c.php

namespace MyVendor\ExtName\Utilities;

use MyVendor\ExtName\Controller\MainController;
use AnotherVendor\ExtName\Controller\MainController as AnotherController;
use function MyVendor\ExtName\Controller\test;

class MainUtility {
    public function foo() {
        MainController::test();
        AnotherController::test();

        test();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM