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

后端 未结 17 1458
抹茶落季
抹茶落季 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:17

    Variables in PHP are cast in three different type:

    Public : values of this variable types are available in all scope and call on execution of you code. declare as: public $examTimeTable;

    Private: Values of this type of variable are only available on only to the class it belongs to. private $classRoomComputers;

    Protected: Values of this class only and only available when Access been granted in a form of inheritance or their child class. generally used :: to grant access by parent class

    protected $familyWealth;

    0 讨论(0)
  • 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
    0 讨论(0)
  • 2020-11-21 08:24

    private - can be accessed from WITHIN the class only

    protected - can be accessed from WITHIN the class and INHERITING classes

    public - can be accessed from code OUTSIDE the class as well

    This applies to functions as well as variables.

    0 讨论(0)
  • 2020-11-21 08:24

    ⚡️ Here is an easy way to remember the scope of public, protected and private.

    PUBLIC:

    • public scope: A public variable/function is available to both objects and other classes.

    PROTECTED:

    • protected scope: A protected variable/function is available to all the classes that extend the current class.
    • No! Objects cannot access this scope

    PRIVATE:

    • private scope: A private variable/function is only visible in the current class where it is being defined.
    • No! Class that extend the current class cannot access this scope.
    • No! Objects cannot access this scope.

    Read the Visibility of a method or variable on PHP Manual.

    0 讨论(0)
  • 2020-11-21 08:25

    Public: is a default state when you declare a variable or method, can be accessed by anything directly to the object.

    Protected: Can be accessed only within the object and subclasses.

    Private: Can be referenced only within the object, not subclasses.

    0 讨论(0)
  • 2020-11-21 08:27

    When we follow object oriented php in our project , we should follow some rules to use access modifiers in php. Today we are going to learn clearly what is access modifier and how can we use it.PHP access modifiers are used to set access rights with PHP classes and their members that are the functions and variables defined within the class scope. In php there are three scopes for class members.

    1. PUBLIC
    2. PRIVATE
    3. PROTECTED

    Now, let us have a look at the following image to understand access modifier access level

    Now, let us have a look at the following list to know about the possible PHP keywords used as access modifiers.

    public :- class or its members defined with this access modifier will be publicly accessible from anywhere, even from outside the scope of the class.

    private :- class members with this keyword will be accessed within the class itself. we can’t access private data from subclass. It protects members from outside class access.

    protected :- same as private, except by allowing subclasses to access protected superclass members.

    Now see the table to understand access modifier Read full article php access modifire

    0 讨论(0)
提交回复
热议问题