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?
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).
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.
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.
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 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With