We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Trailing Slash

Do we really need to use trailing slash ?

$di = new \Phalcon\DI\FactoryDefault();

Because it works just fine without it.

$di = new Phalcon\DI\FactoryDefault();

The only thing I see diferent is syntax highliting color in Sublime.



6.9k
Accepted
answer

If no namespace was declared at the top of the file then it it not necessary to declare the global namespace with the trailing slash

https://php.net/manual/en/language.namespaces.global.php



125.7k
Accepted
answer

That's a leading slash. Trailing would be at the end.

Namespace declarations kind of work like a directory structure. Without a leading slash, the namespace can be thought of as "relative", whereas the leading slash treats the declaration as "absolute". Like @david-duncan said, if you're not inside a namespace, then the relative and absolute namespace declarations will point to the same thing.

If, for example, you have declared the "Foo" namespace at the top of the file, then your second example would be resolved to: \Foo\Phalcon\DI\FactoryDefault.



16.6k

@ quasipickle, you're right ... leading slash ... sorry! Thanks for the info guys.