Search code examples
phpebay-api

How can use two namespaces in one file?


I am using SDK from http://devbay.net/sdk/guides/api/namespace-DTS.eBaySDK.html And I need use Finding and Trading services in one file. How can i declare different namespaces

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

So is any other way to do this ?


Solution

  • You can use 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;
    

    Though to avoid confusion with these newly introduced names, you could fall back to import only \DTS\eBaySDK\Finding and \DTS\eBaySDK\Trading and explicitly use the types there like this:

    use \DTS\eBaySDK\Constants;
    use \DTS\eBaySDK\Finding;
    use \DTS\eBaySDK\Trading;
    
    $fs = new Finding\Services\FindingService();