Uffo
Uffo

Reputation: 10046

Namespace the real meaning

I have read about namespaces and I played a bit with them just now. But I want to make sure that I got this right.

1) So namespaces are here to save as from duplications of name, really good when you add sources from outside into an application for example

2) Is it a good practice to use namespaces, to avoid duplication

3) Is it a good practice to use a namespace like a folder structure, For example if I have Folder/Folder1/Fileone.php to call my namespace like this: namespace Zend/Folder1/Fileone;

I just wanted to make sure that I got this right, and namespaces are basically here to keep as organized and is good to use them know.

Upvotes: 0

Views: 68

Answers (1)

deceze
deceze

Reputation: 522026

  1. Exactly, namespaces make sure there's no conflict between two libraries/code bases/projects which declare the same classes. It's even useful within one application to separate different classes which may logically be given the same name; say View\User and Model\User.

    You can get the same benefit by naming your classes class View_User etc., but this means you always have to use the class by that name. Look at some larger projects like Zend Framework 1.0 to see what a verbose mess this leads to. With namespaces you can use shorter names within the same namespace and alias classes to short names easily.

  2. Yes, it's best to namespace all your code with ProjectName\... or even VendorName\ProjectName\... to make sure you're avoiding collisions with 3rd party code.

  3. Yes, if you correlate folder names to class names (including namespaces), it's easy to use an autoloader. It also simply makes sense.

Upvotes: 2

Related Questions