Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

namespaces

php

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?

like image 302
Webmut Avatar asked Apr 08 '12 13:04

Webmut


People also ask

What are the uses of programming language?

Function: A programming language is a language used to write computer programs, which instruct a computer to perform some kind of computation, and/or organize the flow of control between external devices (such as a printer, a robot, or any peripheral).

Why is this used in programming?

The keyword this is a Java language keyword that represents the current instance of the class in which it appears. It is used to access class variables and methods. Since all instance methods are virtual in Java, this can never be null.

Which programming language has more uses?

JavaScript is the most common coding language in use today around the world. This is for a good reason: most web browsers utilize it and it's one of the easiest languages to learn.

What are the 3 types of programming language?

There are three types of programming languages: machine language, assembly language, and high-level language. Machine language is easier for the computer to understand but harder for the programmer to understand. This is because machine language is simply the language of machines—bits.


1 Answers

+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

like image 112
Baba Avatar answered Nov 13 '22 13:11

Baba