命名名称空间和类的最佳方式是什么
1) 具有类 "Model" 的命名空间 "Domain\Customer"
namespace MyDomain\Customer;
class Model { }
class Repository { }
namespace MyDomain\City;
class Model { }
class Repository { }
2) 具有类 " 的命名空间 "MyDomain"客户模型”?
namespace MyDomain;
class CustomerModel { }
class CustomerRepository { }
namespace MyDomain;
class CityModel { }
class CityRepository { }
3)命名空间“MyDomain\Customer”与类“CustomerModel”?
namespace MyDomain\Customer;
class CustomerModel { }
class CustomerRepository { }
namespace MyDomain\City;
class CityModel { }
class CityRepository { }
或者...
我们当前的环境使用 PHP 自动加载功能,以这种方式包含文件
$o = new MyDomain\Customer\Model.php
将加载文件
MyDomain\Customer\Model.php
实际上,在我工作的地方,我们使用示例#1,导致所有文件都被命名为 Model.php。如果不查看 Eclipse IDE 中的完整路径,就很难知道打开的是哪个模型。
1) A Namespace "Domain\Customer" with a Class "Model"
namespace MyDomain\Customer;
class Model { }
class Repository { }
namespace MyDomain\City;
class Model { }
class Repository { }
2) A Namespace "MyDomain" with a class "CustomerModel" ?
namespace MyDomain;
class CustomerModel { }
class CustomerRepository { }
namespace MyDomain;
class CityModel { }
class CityRepository { }
3) A Namespace "MyDomain\Customer" with a class "CustomerModel" ?
namespace MyDomain\Customer;
class CustomerModel { }
class CustomerRepository { }
namespace MyDomain\City;
class CityModel { }
class CityRepository { }
Or ...
Our current environment use a PHP auto-load function that include file this way
$o = new MyDomain\Customer\Model.php
Will load the file
MyDomain\Customer\Model.php
Actually where I work we use example #1 that cause all file to be named Model.php. It's a bit hard to to know what model is open without looking full path in Eclipse IDE.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类名应该表达它们所代表的内容。名为
Model
的类听起来像是某些模型的通用类。相反,CustomerModel
表示它是客户的模型。当您脱离上下文时,客户也没有明确定义,但在您的特定应用程序中,其含义是明确的。所以我建议选择CustomerModel
。如果您只有
Model
和Repository
类,则仅为这两个类声明整个命名空间是不好的,尤其是当您定义多个两类命名空间时。也许将模型与存储库分开?Class names should express what they represent. A class called
Model
sounds like if it is a general class for certain models.CustomerModel
, in contrast, says that it is the model of a customer. Customer also is not clearly defined when you see it out of the context, but in your particular application, the meaning is clear. So I would suggest to choseCustomerModel
.If you only have
Model
andRepository
classes, it wouldn't be good to declare a whole namespace only for those two classes, especially when you would define several two-class namespaces. Maybe separate the models from the repositories?