如何在一个文件中使用两个命名空间?

How can use two namespaces in one file?

我正在使用 http://devbay.net/sdk/guides/api/namespace-DTS.eBaySDK.html 的 SDK 我需要在一个文件中使用查找和交易服务。 我如何声明不同的命名空间

use \DTS\eBaySDK\Constants;
use \DTS\eBaySDK\Finding\Services;
use \DTS\eBaySDK\Finding\Types;
use \DTS\eBaySDK\Finding\Enums;


use \DTS\eBaySDK\Trading\Services;
use \DTS\eBaySDK\Trading\Types;
use \DTS\eBaySDK\Trading\Enums;

PHP Fatal error: Cannot use DTS\eBaySDK\Trading\Services as Services because the name is already in use

还有其他方法吗?

Namespace 1 { //your logic }

命名空间2{ //你的逻辑

}

您可以使用 aliases:

use \DTS\eBaySDK\Constants;
use \DTS\eBaySDK\Finding\Services as FServices;
use \DTS\eBaySDK\Finding\Types as FTypes;
use \DTS\eBaySDK\Finding\Enums as FEnums;


use \DTS\eBaySDK\Trading\Services as TServices;
use \DTS\eBaySDK\Trading\Types as TTypes;
use \DTS\eBaySDK\Trading\Enums as TEnums;

虽然为了避免与这些新引入的名称混淆,您可以回退到仅导入 \DTS\eBaySDK\Finding\DTS\eBaySDK\Trading 并像这样显式使用那里的类型:

use \DTS\eBaySDK\Constants;
use \DTS\eBaySDK\Finding;
use \DTS\eBaySDK\Trading;

$fs = new Finding\Services\FindingService();