Access modifiers in php

Access modifiers are keywords in object-oriented languages that set the accessibility of classes, methods, and other members.

Let me explain further;

In a family there are some information that can't be shared with anyone outside your family. There are also some information that can be shared with anyone outside your family, This just depends on the policy in your family. Makes sense?

In php there are 3 access modifiers and they are: private, protected, public.

Public Access Modifier

If a method or property is declared as public this means it can be accessed from anywhere. When i say anywhere i mean anywhere in your application. either by reinstantiating the class to create an object or by extending the class.

class Family
{
    public function goodNews()
    {
          return "We did it";
    }
}

return (new Family())->goodNews();
//returns "We did it"

The above example shows that goodNews Method (A method is a function inside a class) can be accessed outside the class.

Private Access Modifier

If a method or data members is declared as private this means it can not be accessed from outside that class. The method or property cannot be accessed from a child class also.

   //Example 1
    class Family
    {
    private function familyNews()
    {
          return "This message is for only family";
    }

    public function tellSiblings()
    {
         return $this->familyNews();
    }
   }
//return (new Family())->familyNews(); //Uncaught Error: Call to private method Family::familyNews()
//return (new Family())->tellSiblings(); // This message is for only family

From the above illustration, you see familyNews can't be accessed outside the class because it is private, that information is very personal to the class and it can only be accessed inside that class like so in tellSiblings Method.

//Example 2
class Family
{
    private function familyNews()
    {
          return "This message is for only family";
    }
    public function tellSiblings()
    {
         return $this->familyNews();
    }

 }

class Friend extends Family
{ 
    public function gossip()
    {
          return $this->familyNews();
    }
 }

//return (new Friend())->gossip(); //Call to private method Family::familyNews() from scope Friend

From the above illustration, we are calling familyNews from a sub class. it throws an error because it is private. It can not be accessed from a child class because it is Private, this brings us to protected access modifier.

Protected Access Modifier

Protected is just like private but in this case a child class can access a protected method or data member. It cannot also be accessed outside that class.

class Family
{
    protected function familyNews()
    {
          return "This message is for only family";
    }

    public function tellSiblings()
    {
         return $this->familyNews();
    }
 }

class Friend extends Family
{ 
    public function gossip()
    {
          return $this->familyNews();
    }
 }

//return (new Friend())->familyNews(); //Uncaught Error: Call to protected method Family::familyNews() from global scope
//return (new Friend())->gossip(); // This message is for only family

Conclusion

A public access modifier is used when method or properties should be accessed on a global scope, meaning they should be accessed outside the class.

A private access modifier is used when method or properties should only be accessed inside that same class

A protected access modifier is used when method or properties should only be accessed inside child class or sub classes.