bb00x Posted May 22, 2022 Share Posted May 22, 2022 I can't understand the notion of the namespace clearly , anyone help me Quote Link to comment https://forums.phpfreaks.com/topic/314828-how-to-use-namespace-in-php/ Share on other sites More sharing options...
ginerjm Posted May 22, 2022 Share Posted May 22, 2022 Me neither. I think it might have something to do with large projects that involve many scripts and many variables and I'm thinking namespace is a way of avoiding collisions with names. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314828-how-to-use-namespace-in-php/#findComment-1596562 Share on other sites More sharing options...
Solution requinix Posted May 23, 2022 Solution Share Posted May 23, 2022 You'd use namespaces with your classes for the same reason that you'd use directories with your files: because giving each thing some long and complicated name just so that they all stay unique is a pain. Practically speaking, if you use underscores in your class names then you're already using namespaces. 1 Quote Link to comment https://forums.phpfreaks.com/topic/314828-how-to-use-namespace-in-php/#findComment-1596563 Share on other sites More sharing options...
gizmola Posted May 23, 2022 Share Posted May 23, 2022 To add a bit to this, Namespaces solve one major problem, which is class and function name conflicts. Here's a simple example: namespace Gizmola; function substr($string) { return \substr($string, 0, 3); } $test = '12345678910'; echo substr($test); With a namespace, I'm able to redefine a built in function, as well as use the built in function in my customized version of substr. I can then include my function and use it as a library in other scripts: use function Gizmola\substr as substr; echo substr('Now is the time for us to use namespaces'); This becomes even more valuable when you are dealing with class libraries. Without namespaces, the use of a library would mean that every single class across all the libraries you might want to be using, would need to have a unique name. Namespaces solve this issue, and allow you to organize your code, and refer to other classes in an unambiguous way. I can reference any class definition by including its namespace. // Logger.php namespace Gizmola; class Logger { public function log() { // ... } } Some other code that wants to use the Logger class. namespace MyApp; require_once('path/to/gizmola/Logger.php'); use Gizmola\Logger; $logger = new Logger(); $logger->log(); If for some reason I end up using a class library that also has a Logger class, like Seldaek\Monolog, I can do that via the ability to alias a class or function when I use it: namespace MyApp; use Gizmola\Logger; use Monolog\Logger as AppLogger; $logger = new Logger(); $appLogger = new AppLogger('main'); $logger->log(); $appLogger->warning('Problem with MyApp'); Alternatively, I could reference it directly via it's namespace to get around the conflict: namespace MyApp; $logger = new Gizmola\Logger(); $logger->log(); $appLogger = new Monolog\Logger('main'); $appLogger->warning('Problem with MyApp'); The other benefit of namespaces, is that by applying a convention to the way you map a namespace to a directory structure, a class autoloader can determine the location of a class and load it at runtime. This is where PSR-0 and now PSR-4 come into play. Library authors who conform the directory structure and location of their code to these standards will allow their library to be easily integrated into any project. These standards were designed to incorporate the organizational structure of as many pre-existing libraries as possible, so there is some interesting code in there, but for most people, creating your own class is as simple as putting it into a directory structure that more or less maps to the namespace. Composer is able to install a component library, and make that available to your app with its own libraries, and provide an autoloader and static class map for you to use if you want it, relieving you of having to require classes or be concerned about placing them in specific includable directories on your server, as you had to do in the olden days before namespaces were introduced. Quote Link to comment https://forums.phpfreaks.com/topic/314828-how-to-use-namespace-in-php/#findComment-1596594 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.