Skip to content

Use of protected class members is discouraged #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ivanweiler opened this issue Nov 28, 2016 · 3 comments
Closed

Use of protected class members is discouraged #27

ivanweiler opened this issue Nov 28, 2016 · 3 comments

Comments

@ivanweiler
Copy link

ivanweiler commented Nov 28, 2016

Hello Magento,

Just a question, can you please explain why using protected is considered bad practice in Magento:
"WARNING | Use of protected class members is discouraged."

We're trying to prepare one extension for marketplace, and we deliberately used protected on few places to give other developers a chance to extend and change behavior of some classes if ever needed. If we change everything to private, any kind of class extensibility is harder to achieve.

I'm just not sure if discouraging protected is a good idea, they have their own purpose, so we would like to know logic behind this so we could follow best practices better :)

Thanks, Ivan

@danslo
Copy link

danslo commented Nov 28, 2016

As far as I understood, it's due to the principles of Composition over inheritance.

So instead of using protected methods, split this functionality into a different class and inject it. Then it will be easier to override this functionality via plugins, because this separate class still needs to expose at least 1 public method. Preferences are not preferable because they suffer from same issues as M1's rewrites.

Another thing worth mentioning is that these are specifically marked as warnings rather than errors. It's almost impossible to create extensions that don't make use of protected properties/methods because of the state of the Magento core itself.

@lenaorobei
Copy link
Contributor

@ivanweiler closing this issue as duplicate #7
You can find more information here and here.

@tiagosampaio
Copy link
Member

Hey guys, perfect explanation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants