Webmut
Webmut

Reputation: 2858

Usage of 'use' or 'using' in programming languages

I always thought the main goal of a namespace is to prevent name collision and ambiguity.

#1 problem fixed by namespaces from php.net:

Name collisions between code you create, and internal PHP classes/functions/constants or third-party classes/functions/constants.

However, most languages implement the "use" keyword in some way to alias or import other namespace to the current one. I know how it works, but I don't understand why such functionality is ever used.

Isn't using a 'use' keyword effectively defeating the purpose of a namespace?

namespace core\utils;

class User {
    public static function hello(){
        return "Hello from core!";
    }
}
//---------------------------------------------------

namespace core2\utils;

class User {
    public static function hello(){
        return "Hello from core2!";
    }
}
//---------------------------------------------------

namespace core2;

//causes name collision, we now have two different classes of type 'utils\User'
use core\utils; //without this line the result is 'Hello from core2'

class Main {
    public static function main(){
        echo utils\User::hello();
    }
}

Main::main();
//outputs Hello from core!
?>

Am i missing something or is usage of 'use' keywords really generally discouraged?

Either way, under what circumstances is it actually a good idea to sacrifice the encapsulation?

I used to use use, but now I am not sure when use should be used.

Edit: Alright let me get this straight: If I use 'use' to get short name, how is that better than just declaring the class in global namespace? See below:

namespace core\utils\longname {    
    class User {} //declare our class in some namespace
}

//------------------Other File---------------------------
namespace { //in another file import our long name ns and use the class
    use core\utils\longname\User as User;
    new User();
}

^ What is the advantage of namespacing like that against this declaration:

namespace {    
    class User {} //declare our class in global namespace
}

//------------------Other File---------------------------
namespace { //in another file just use the class
    new User();
}

Is there any difference at all between the two?

Upvotes: 11

Views: 219

Answers (1)

Baba
Baba

Reputation: 95121

+1 Very Interesting question

My Opinion

The keyword use as so many uses and functionality imagine this

use core\utils\sms\gateway\clickatell\http\SmsSender  as SmsCSender 
use core\utils\sms\gateway\fastSMS\ftp\Smssender as SmsFSender 

Now Compare

if(!SmsCSender::send($sms))
{
    SmsFSender::send($sms);
}

To

if(!core\utils\sms\gateway\clickatell\http\SmsSender::send($sms))
{
    core\utils\sms\gateway\fastSMS\ftp\SmsSender::send($sms);
}

Conclusion

Without namespace and use i would not be able to achieve such a clean readable code so what i think is that namespace and use complement each other rather than 'use' defeating the purpose of a namespace

Upvotes: 3

Related Questions