What is the difference between public, private, and protected?

后端 未结 17 1558
抹茶落季
抹茶落季 2020-11-21 07:54

When and why should I use public, private, and protected functions and variables inside a class? What is the difference between them?<

17条回答
  •  说谎
    说谎 (楼主)
    2020-11-21 08:19

    You use:

    • public scope to make that property/method available from anywhere, other classes and instances of the object.

    • private scope when you want your property/method to be visible in its own class only.

    • protected scope when you want to make your property/method visible in all classes that extend current class including the parent class.

    If you don't use any visibility modifier, the property / method will be public.

    More: (For comprehensive information)

    • PHP Manual - Visibility

提交回复
热议问题