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

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

    For me, this is the most useful way to understand the three property types:

    Public: Use this when you are OK with this variable being directly accessed and changed from anywhere in your code.

    Example usage from outside of the class:

    $myObject = new MyObject()
    $myObject->publicVar = 'newvalue';
    $pubVar = $myObject->publicVar;
    

    Protected: Use this when you want to force other programmers (and yourself) to use getters/setters outside of the class when accessing and setting variables (but you should be consistent and use the getters and setters inside the class as well). This or private tend to be the default way you should set up all class properties.

    Why? Because if you decide at some point in the future (maybe even in like 5 minutes) that you want to manipulate the value that is returned for that property or do something with it before getting/setting, you can do that without refactoring everywhere you have used it in your project.

    Example usage from outside of the class:

    $myObject = new MyObject()
    $myObject->setProtectedVar('newvalue');
    $protectedVar = $myObject->getProtectedVar();
    

    Private: private properties are very similar to protected properties. But the distinguishing feature/difference is that making it private also makes it inaccessible to child classes without using the parent class's getter or setter.

    So basically, if you are using getters and setters for a property (or if it is used only internally by the parent class and it isn't meant to be accessible anywhere else) you might as well make it private, just to prevent anyone from trying to use it directly and introducing bugs.

    Example usage inside a child class (that extends MyObject):

    $this->setPrivateVar('newvalue');
    $privateVar = $this->getPrivateVar();
    
    0 讨论(0)
  • 2020-11-21 08:30

    Considering 'when':
    I tend to declare everything as private initially, if I'm not exactly sure. Reason being, that it's usually much easier to turn a private method public than the other way round. That's because you can at least be sure that the private method hasn't been used anywhere but in the class itself. A public method may already be in use everywhere, possibly requiring an extensive re-write.

    Update: i go for a default of protected nowadays, because I've come to find that it's good enough for encapsulation and doesn't get in the way when I'm extending classes (which i try to avoid anyway). Only if i have a good reason to use the other two, i will.

    A good reason for a private method would be one that implements something inherent to that object that even an extending class should not change (factual reason, in addition to encapsulation, like internal state management). Eventually, it's still easy enough to track down where a protected method is being used usually, so i default to protected nowadays. Maybe not 100% objective "in the trenches" experience, I admit.

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

    It is typically considered good practice to default to the lowest visibility required as this promotes data encapsulation and good interface design. When considering member variable and method visibility think about the role the member plays in the interaction with other objects.

    If you "code to an interface rather than implementation" then it's usually pretty straightforward to make visibility decisions. In general, variables should be private or protected unless you have a good reason to expose them. Use public accessors (getters/setters) instead to limit and regulate access to a class's internals.

    To use a car as an analogy, things like speed, gear, and direction would be private instance variables. You don't want the driver to directly manipulate things like air/fuel ratio. Instead, you expose a limited number of actions as public methods. The interface to a car might include methods such as accelerate(), deccelerate()/brake(), setGear(), turnLeft(), turnRight(), etc.

    The driver doesn't know nor should he care how these actions are implemented by the car's internals, and exposing that functionality could be dangerous to the driver and others on the road. Hence the good practice of designing a public interface and encapsulating the data behind that interface.

    This approach also allows you to alter and improve the implementation of the public methods in your class without breaking the interface's contract with client code. For example, you could improve the accelerate() method to be more fuel efficient, yet the usage of that method would remain the same; client code would require no changes but still reap the benefits of your efficiency improvement.

    Edit: Since it seems you are still in the midst of learning object oriented concepts (which are much more difficult to master than any language's syntax), I highly recommend picking up a copy of PHP Objects, Patterns, and Practice by Matt Zandstra. This is the book that first taught me how to use OOP effectively, rather than just teaching me the syntax. I had learned the syntax years beforehand, but that was useless without understanding the "why" of OOP.

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

    Visibility Scopes with Abstract Examples :: Makes easy Understanding

    This visibility of a property or method is defined by pre-fixing declaration of one of three keyword (Public, protected and private)

    Public : If a property or method is defined as public, it means it can be both access and manipulated by anything that can refer to object.

    • Abstract eg. Think public visibility scope as "public picnic" that anybody can come to.

    Protected : when a property or method visibility is set to protected members can only be access within the class itself and by inherited & inheriting classes. (Inherited:- a class can have all the properties and methods of another class).

    • Think as a protected visibility scope as "Company picnic" where company members and their family members are allowed not the public. It's the most common scope restriction.

    Private : When a property or method visibility is set to private, only the class that has the private members can access those methods and properties(Internally within the class), despite of whatever class relation there maybe.

    • with picnic analogy think as a "company picnic where only the company members are allowed" in the picnic. not family neither general public are allowed.
    0 讨论(0)
  • 2020-11-21 08:35

    The mentioned keywords are access modifiers and help us implement Encapsulation (or information hiding). They tell the compiler which other classes should have access to the field or method being defined.

    private - Only the current class will have access to the field or method.

    protected - Only the current class and subclasses (and sometimes also same-package classes) of this class will have access to the field or method.

    public - Any class can refer to the field or call the method.

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