关于继承关系的一点小建议

问题咨询 · mztest · 于 3年前 发布 · 810 次阅读

Fecmall V1.12.1 Fecyo V.1.9.11

fecyo\models\mysqldb\customer\CustomerRegister extends fecshop\models\mysqldb\Customer
fecyo\models\mysqldb\Customer extends \fecshop\models\mysqldb\Customer

common\local\local_models\mysqldb\Customer extends \fecyo\models\mysqldb\Customer
common\local\local_models\mysqldb\customer\CustomerRegister extends \fecyo\models\mysqldb\customer\CustomerRegister

在使用过程中发现有类似的继承, 通常情况下,在common里边的类会是整个网站的公共类,如果我在 common\local\local_models\mysqldb\Customer 这个类里写上一些公共方法,比如getFullName(), beforeSave()等,

那么这个时候 common\local\local_models\mysqldb\customer\CustomerRegister 它就无法使用上述的公共方法,尽管最终类是 \fecshop\models\mysqldb\Customer,

是否可以进行一些约定,在插件中总是应该扩展COMMON目录下面的类,这样当我们需要二次开发时就会相对方便一些。

不知道我有没有表达清楚。

共收到 4 条回复
Fecmall#13年前 0 个赞

看不懂什么意思,如下:

common\local\local_models\mysqldb\Customer 这个类里写上一些公共方法,比如getFullName(), beforeSave()等,

那么这个时候 common\local\local_models\mysqldb\customer\CustomerRegister 它就无法使用上述的公共方法,尽管最终类是 \fecshop\models\mysqldb\Customer,

Fecmall#23年前 0 个赞

好像明白你的意思了, 你这个比较特殊

你可以使用yii2万能替换方法,classMap的方式,替换 fecshop\models\mysqldb\Customer

详细参看:https://www.fecmall.com/doc/fecshop-guide/develop/cn-2.0/guide-fecmall-rewrite-func.html#7yii2classclassmapfecmall

对于classMap的方式,注意,是替换,而不是重写

大致的原理,通过namespace寻找类文件的时候,通过配置指定到新的类文件。因此无法继承,只能替换。

当然,这种方式也有劣势,就是fecmall假设更新了文件,您也需要更新。

mztest#33年前 0 个赞

嗯,看了您的文档可以使用替换类,也正是因为你上面提到的缺点才没有使用。

所以如果约定插件不能直接继承fecshop中的基类, 而总是应该通过继承common目录的类,似乎可能从一定程序上解决上面的问题。

Fecmall#43年前 0 个赞

@mztest #3楼

1.建议,往往意味着给与一定的解决方案,如果你有,可以以这个为例子,写一下你的具体解决方案。

你可以自己多思考思考具体怎么搞嘛?

2.fecmall有services,这些东西可以在services里面解决

controller block层都不能直接调用model层,而是通过调用services来实现,你可以在services里面实现,为什么要跑到models里面实现?

3.你说的的确有一定道理,但是很多时候做不到完美,如果以这个为标准,fecmall肯定很多类似的地方。

添加回复 (需要登录)
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册
Your Site Analytics