Best to use Private methods or Protected methods?

后端 未结 10 2331
[愿得一人]
[愿得一人] 2020-12-03 09:52

In a lot of my PHP projects, I end up with classes that have non-public functions that I don\'t intend to extend.

Is it best to declare these as protected, or privat

10条回答
  •  忘掉有多难
    2020-12-03 10:31

    If you intend to build a class for inheritance, you must design it that way, i.e. make those methods protected that allow other developers to change the behavior of the class along the lines that you intended. Simply making all methods protected is not a very good design. Bear in mind that all protected methods become part of the public API, so if you change things later, you will break other people's code.

    In general, if you're not designing for inheritance, you should prohibit it.

提交回复
热议问题