L
L
link002015-03-07 01:01:25
Yii
link00, 2015-03-07 01:01:25

About rewriting methods/properties in PHP. What is the best way to do this?

I welcome everyone.
1. For example, there is a product card. And there is a base class Product. It has properties, for example: price, alias, descriptions, images. There are methods, CRUD, price recalculation with discounts / coupons, multi-level pricing, image cropping, etc.
2. What is the point. It is necessary that there was something like "plugins". For example, you need a plugin that will replay to product_type+product_name in it before displaying the description. Or, a plug-in that will insert into the "alias" field will translate the "product name" fields.
3. Logically, a solution suggests itself - to extend the Product class, and to shove the necessary functionality into the extended class. But there can be a LOT of plugins. Different plugins can be used on different domains/pages/categories. Therefore, I want a more elegant solution.
4. I see it like this (fantasizing =)))): a "plugins" folder is created, *.php files are thrown into it, each of which is logically a plugin, and at the language level is a class. The Product class, in which ONLY NECESSARY methods / properties are simply overwritten. (PHP will write "Fatal error: Cannot redeclare class ..." on such an attempt to rewrite)
Actually, this is the question. 1) How to solve such a problem more gracefully on bare PHP 2) It’s the same on Yii (maybe there is something provided for this kind of problems)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sanex3339, 2015-03-07
@link00

Behaviors
habrahabr.ru/post/208328
www.yiiframework.com/doc-2.0/yii-base-behavior.html

Q
Quber, 2015-03-07
@Quber

Traits, no?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question