Best to use Private methods or Protected methods?

后端 未结 10 2305
[愿得一人]
[愿得一人] 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:32

    Personally, I find it proper to make as much private as you possibly can. I just look at each method and ask myself if I want a derived class to be able to call it. Making everything protected leaves open the door to having methods called incorrectly.

    I guess it comes down to the questions "Is everything forbidden unless specifically permitted" or "Is everything permitted unless specifically forbiffffden.

    One additional factor is that it's easy to make a private method protected in a future release. It's almost impossible to privatize a method once it's made protected, as you never know what other code you invalidate.

提交回复
热议问题