yiisoft/di

Public API method is protected

armpogart opened this issue · 6 comments

Yiisoft\Di\Container::set method is protected despite the fact that it is used in service providers to set dependencies. I'm not even sure how it works, I've detected the problem while using IDE autocomplete.

I would also appreciate if somebody explained me how it works while being protected and doesn't trigger error.

Hmm... as far as I see it is possible to access protected and even private properties and methods of the other object as long as they share the same parent (are of the same type) and here ServiceProvider and Container both extend from AbstractContainerConfigurator, but I still think that it is incorrect in terms of strict OOP (though PHP OOP allows it) and we need to make the set method public.

Here is excerpt from PHP documentation:

Objects of the same type will have access to each others private and protected members even though they are not the same instances. This is because the implementation specific details are already known when inside those objects.

Do I need to make a quick PR?

I still think that it is incorrect in terms of strict OOP (though PHP OOP allows it)

It's not. That's the friendly classes concept. Works the same in all OO languages with inheritance such as Java.

Hmm... I don't have much experience in Java, but C++ doesn't have such thing for sure without explicit friend class declaration (that's obvious as there's no runtime in C++ and the compiler can't do such magic) and C# that works with runtime similar to JVM also doesn't have a such thing. That's why I thought that it is not a good OO pattern.

Anyways the point is that we don't have any autocomplete there. Is there any specific reason why the Container::set method is not public API?

The idea is the same as with event dispatcher. To disallow setting container runtime and allow it only at configuration time.

Decided to keep it as is for a while.