Skip to content

Understanding OOP Visibility (Public, Private, and Protected): A Guide with PHP

Understanding OOP Visibility (Public, Private, and Protected): A Fun Guide with PHP

Object-oriented programming (OOP) brings structure and clarity to coding by bundling related properties and methods into individual objects. One of its core principles is controlling access to an object’s properties and methods, known as “visibility.” While this might sound like a high-level security protocol at the Pentagon, it’s actually more like deciding who gets access to the remote control at home on movie night. In OOP, especially in languages like PHP, there are three main types of visibility: public, protected, and private. Let’s break these down with some amusing, everyday examples.

OP Visibility Public Private and Protected

Public: The Open Book

Imagine you’re at a party and there’s a bowl of chips on the table. Anyone can come up and take some chips; they’re public. In PHP, public properties and methods are like those chips. They’re accessible from anywhere – other objects, classes that extend the class, even outside the class. Here’s how it looks:

class Party {
    public $chips = 'Salted';
    
    public function shareChips() {
        return "Here, have some $this->chips chips!";
    }
}

In this scenario, anyone can access the type of chips and the method to share them, no questions asked.

Protected: Family Secrets

Now, think of a family recipe that’s shared only with family members. It’s protected. In PHP, protected properties and methods are accessible within the class itself and by classes that extend it, but not outside of these.

class Family {
    protected $recipe = 'Secret Salsa';
    
    protected function shareRecipe() {
        return "Shh... It's a secret recipe!";
    }
}

class Member extends Family {
    public function getRecipe() {
        return $this->shareRecipe(); // This works
    }
}

Trying to access $recipe or shareRecipe() from outside these bounds is like asking for the secret family recipe at the party – a definite no-no.

Private: Diary Lock

Lastly, private properties and methods are the diary with a lock. Only you, the class where they are declared, can access them. Not even child classes that extend it have the key.

class Diary {
    private $thoughts = 'My secret thoughts';
    
    private function lockThoughts() {
        return "No one can see my $this->thoughts!";
    }
    
    public function shareThoughts() {
        return $this->lockThoughts(); // Only I can access my thoughts
    }
}

Even if someone inherits the diary (extends the class), the private thoughts remain inaccessible.

Remembering the Visibility Levels

To remember these, think of public as a public park, protected as your home, and private as your diary. The park is open to everyone, your home is open to you and your family, and your diary is only open to you.

With a Twist of Humor

  • Public: Like wearing a T-shirt with your favorite band. Anyone can see it and ask you about it.
  • Protected: Like your home Wi-Fi password. Only family and friends who visit can use it.
  • Private: Like your browser history. Only you should have access to it, ideally.

Conclusion

Understanding visibility in OOP is crucial for proper data encapsulation and security in your code. Whether you’re managing a PHP project or navigating the waters of OOP in another language, remembering these concepts can be as easy as thinking about chips, family recipes, and diaries. Keep these examples in mind, and you’ll navigate OOP visibility like a pro!

Views: 14

Leave a Reply

Your email address will not be published. Required fields are marked *