继承我有一个基础对象(form_field_base),其被扩展/其它对象的形式继承:PHP和对象 - 延伸的公共对象,该对象的其他对象从
class form_field_base {
// Lots of code
}
class form_field_text extends form_field_base {
// Lots of code
}
class form_field_email extends form_field_text {
// Extending the text object to update validation, and set input type="email"
}
class form_field_file extends form_field_base {
// Lots of code, for example uploading files
}
的“form_field_base”规定所有表单字段类型使用的帮助器方法,例如html()函数调用特定对象(form_field_email :: html_input)以获取字段,然后将其放入具有标准标记的字符串中,等等。
所有这些对象都被许多项目使用。
但是,我正在处理的这个最新项目需要定制“form_field_base”对象以允许设置一些帮助文本,这是没有其他项目需要的功能,并且如果将来的项目需要,它可能会完成不同。
那么这应该如何组织?
理想情况下,我不会拥有“form_field_base”的完整副本,因为这会导致代码重复。
而且它似乎是相当多的开销有假中间对象:
class form_field_base_common {
// Lots of code
}
class form_field_base extends form_field_base_common {
// By default is empty
}
class form_field_text_common extends form_field_base {
// Lots of code
}
class form_field_text extends form_field_text_common {
// ...
}
class form_field_email_common extends form_field_text {
// Extending the text object to update validation, and set input type="email"
}
class form_field_email extends form_field_email_common {
// ...
}
class form_field_file_common extends form_field_base {
// Lots of code, for example uploading files
}
class form_field_file extends form_field_file_common {
// ...
}
以每一个都有它自己的文件,该文件是自动加载(无论是从项目的具体位置,如果它存在,或者来自所有项目都可以访问的公共文件夹)...已经有8个文件需要被发现,打开,解析等,只是为了支持表单。
苏利必须有更好的方法吗?
您是否尝试过私人方法和数据?或者你是什么意思?你是否需要隐藏来自后代的新功能,或者是否使用相同代码库的其他项目? – J0HN
更多的是,“form_field_base”已经非常大,并且为一个小项目(当它被许多项目使用时)添加更多的功能/代码似乎是浪费。 –