这个类结构在php中应该怎么写呢?组合、继承、装饰器?
我有一个数据库,其中人们可能同时是公司的员工、客户和成员。我的管理系统允许我在独立页面上编辑 Person
、Employee
、Customer
和 Member
对象的属性。
现在,我希望能够呼叫 Person
,但能够在同一页面上查看和编辑其员工、客户和成员属性。为这个场景创建一个视图是微不足道的,但我想用基类、继承、组合和聚合等的正确规范来正确地完成它。
我确实明白,如果没有人,所以对于我的管理页面,我想我应该创建一个新类 UberDude
,它是 人员、员工、客户和成员类别的组成。我还想使用这个新类作为集合的一部分,以便我可以绘制所有人的表格并查看他们是员工、客户还是会员。有些事情似乎不对劲,我无法计算出类结构的代码。有什么想法吗?
I have a database where people might simultaneously be employees, customers and members of a company. My adminstration system allows me to edit the properties of the Person
, Employee
, Customer
and Member
objects on independent pages.
Now, I'd like to be able to call up an Person
but be able to view and edit their employee, customer and member properties on the same page. Creating a view for this scenario is trivial, but I'd like to do it properly with the correct specification of base class, inheritances, compositions and aggregates, etc.
I do understand that the employees, customers and members can't exist without the person, so for my admin page, I'm thinking I should create a new class UberDude
that is a composition of the person, employee, customer and member classes. I would also like to use this new class as part of a collection so that I could draw a table of all persons and see whether they are employees, customers or members. Something doesn't seem right and I can't work out the code for the class structure. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来像是继承问题。您有一个
人
,但是可以用更具体的术语来描述该人,例如员工
或客户
。通常在面向对象的编程中,您从基类(在本例中为
Person
)开始,然后在扩展子类中添加属性。因此,一个Person
可能有一个名字,一个Employee
可能有一个员工编号,但如果他们是一个Customer
,这就没有意义了。您可以做的是实现一个装饰器模式,然后继续构建您的
Person
类。我不确定您目前如何在当前应用程序中构建人员的个人资料,但它在伪代码中看起来如下所示:这将实例化您的
Person
类,然后将其传递给您的Employee
类用特定于员工的附加属性和方法进行修饰。Sounds like an inheritance issue. You have a
Person
, but then that person can be described in more specific terms such asEmployee
orCustomer
.Normally in object-orientated programming, you start with your base class (in this instance,
Person
) and then add properties in extending sub-classes. So aPerson
may have a name, anEmployee
may then have an employee number, but this wouldn't make sense if they were aCustomer
.What you could do is implement a decorator pattern, and just keep building on your
Person
class. I'm not sure how you're currently building people's profiles in your current application, but it would look something as follows in pseudo code:This would instantiate your
Person
class, then that would be passed to yourEmployee
class to be decorated with additional properties and methods that are specific to an employee.