0

我已经阅读了有关名称空间的内容,并且刚刚对它们进行了一些尝试。但我想确保我做对了。

1) 所以命名空间在这里是为了保存名称的重复,例如,当您将来自外部的源添加到应用程序中时非常好

2)使用命名空间是否是一种好习惯,以避免重复

3)使用像文件夹结构这样的命名空间是一种好习惯吗,例如,如果我有 Folder/Folder1/Fileone.php 像这样调用我的命名空间: namespace Zend/Folder1/Fileone;

我只是想确保我做对了,并且命名空间基本上在这里保持井井有条,并且知道使用它们很好。

4

1 回答 1

2
  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.
于 2013-08-13T12:13:59.183 回答