Yii2 enchacement proposal: Service classes as traits

Currently, I see in Yii following problems if we want to use BaseObject, Component and other base features:
we can not write a framework agnostic code
we can not have own class hierarchy

The solution can be to convert service classes like BaseObject and Component to traits and put them in a separate package.

There is already yii framework version 3 somewhere near: Yii 2.0.28, extensions and Yii 3 progress
It is much better than you can imagine :slight_smile:

I analyzed Yii3 sample app, and I doubt that Yii3 going the right way. In my opinion, it breaks the most important (in PHP) principles DRY and KISS. SOLID principles solves problems in compiled languages like Java or C#, and these problems (regid stucture because of type safety) do not exists in PHP. PHP has other problems, too much flexibility and loose typing, and needs other patterns. Some of them are implemented in Yii2, some not invented yet.

Could you explain what SOLID principles YII3 breaks? And why do you think that SOLID solves better for compiled languages?
thanks

I did not say Yii3 breaks SOLID principles, I said it breaks DRY, KISS.

It will be interesting to see what we get in Yii3.
But I think that for most projects in PHP Yii2 concept (without LID) would be enough.

My criticism applied actually to wide use of Interfaces in PHP (ID in solid). I believe there should be more traits. Composition considered as better approach then inheritance, but it is undiscovered territory in PHP world, so there are lots of risks also.

I actually would prefer to do less work/typing then more (as I see Yii3 moving to more typing approach). We get better flexibility and testability, but do I need it?

  1. Sometimes :slight_smile: but I always can solve it just by coping class hierarhy and change needed part. (we always get PHP source code, what is false for compiled languages - why in my opinion all this SOLID(D) exists)
  2. PHP used mostly for web development, and unit testing in my opinion not so effective there because there is a lot of UI, DB and other external services. I myself prefer functional testing. But for this we do not need all this SOLID(D).

Moreover only since 7.4 we can implement real SOLID (actually D), because PHP before 7.4 does not support covariance and contravariance in Interfaces. But its comming.

Blockquote
I believe there should be more traits. Composition considered as better approach then inheritance, but it is undiscovered territory in PHP world, so there are lots of risks also.

I don’t think traits are mutually exclusive to interfaces.
Traits are for sharing common functionalities:
you can inject Traits in any classes using single inheritance and you can use Interface to regulate what classes must do.
Interface is a what a class must implements(in terms of methods) and not how is implemented.
So both are extremly useful!

Blockquote
I actually would prefer to do less work/typing then more (as I see Yii3 moving to more typing approach). We get better flexibility and testability, but do I need it?

Completely agree

Agree about traits + interfaces.

In Yii3 developers tring to use pure interfaces. This is hands down extrimely flexible solution, but when we create our own implementation we need more knowledge and time or we should have simplified original implementation. Old way solution was en abstract class + some interfaces, what I personally used to, but this creates problems in languages with single inheritance.
So probably abstraction with interfaces for flexibility + traits for code inheritance and code re-use would be optimal solution.
Did you have experience with this approach in PHP?

Yes, I did and I do now and you too. YII2 yii\db\QueryInterface and yii\db\ActiveQueryTrait.