๐Ÿ” PHP Access Modifiers โ€“ Control Visibility with public, private, and protected

Learn how to use access modifiers in PHP OOP to control the visibility of class properties and methods for better security and encapsulation.


๐Ÿงฒ Introduction โ€“ What Are Access Modifiers in PHP?

Access modifiers (also called visibility keywords) define how accessible class properties and methods are from outside or within a class. PHP provides three main access levels:

  • public โ€“ Accessible from anywhere
  • private โ€“ Accessible only inside the class
  • protected โ€“ Accessible inside the class and by child classes

๐ŸŽฏ In this guide, youโ€™ll learn:

  • The meaning and use of public, private, and protected
  • When to use each modifier
  • How visibility supports encapsulation
  • Real-world examples and best practices

๐Ÿ”“ 1. Public โ€“ Accessible from Anywhere

class User {
    public $name = "Alice";

    public function sayHello() {
        echo "Hello, $this->name!";
    }
}

$user = new User();
echo $user->name; // โœ… Accessible
$user->sayHello(); // โœ… Accessible

โžก๏ธ public members can be accessed from anywhere, including outside the class.
โžก๏ธ Best for methods or properties you intentionally expose.


๐Ÿ” 2. Private โ€“ Accessible Only Within the Class

class Account {
    private $balance = 1000;

    public function getBalance() {
        return $this->balance;
    }
}

$acc = new Account();
// echo $acc->balance; โŒ Error: Cannot access private property
echo $acc->getBalance(); // โœ… 1000

โžก๏ธ private members are only accessible within the class itself.
โžก๏ธ Ideal for sensitive data like passwords, tokens, or internal states.


๐Ÿ›ก๏ธ 3. Protected โ€“ Accessible Within Class and Subclasses

class Vehicle {
    protected $type = "car";
}

class Bike extends Vehicle {
    public function showType() {
        return $this->type;
    }
}

$b = new Bike();
echo $b->showType(); // โœ… car
// echo $b->type; โŒ Error: Cannot access protected property

โžก๏ธ protected members can be accessed by the class itself and any subclass.
โžก๏ธ Useful for extending functionality while keeping it hidden from the outside.


๐Ÿงช 4. Method Visibility Example

class Logger {
    private function log($msg) {
        echo "[LOG]: $msg";
    }

    public function info($msg) {
        $this->log($msg); // โœ… Internal call is allowed
    }
}

$log = new Logger();
// $log->log("Test"); โŒ Error: Private method
$log->info("Test"); // โœ… [LOG]: Test

โžก๏ธ Apply visibility to methods just like properties.
โžก๏ธ Encapsulate behavior while exposing only safe methods externally.


๐Ÿ” 5. Inheritance + Access Modifier Behavior

ModifierSame ClassSubclassOutside Class
publicโœ…โœ…โœ…
protectedโœ…โœ…โŒ
privateโœ…โŒโŒ

๐Ÿ“˜ Choose the visibility level based on how and where the member should be accessed.


๐Ÿง  Best Practices

  • โœ… Keep properties private or protected by default
  • โœ… Use public methods as controlled interfaces
  • โœ… Use getter and setter methods to interact with private data
  • โŒ Avoid making every property public
  • โœ… Encapsulate internal logic and expose only necessary APIs

๐Ÿ“Œ Summary โ€“ Recap & Next Steps

Access modifiers help you write secure and maintainable OOP code by controlling how data and behavior are accessed. They enforce encapsulation, a key pillar of object-oriented programming.

๐Ÿ” Key Takeaways:

  • public โ†’ access anywhere
  • private โ†’ only within the class
  • protected โ†’ within class + subclasses
  • Use visibility to encapsulate data and protect your class structure

โš™๏ธ Real-World Use Cases:
Secure payment gateways, data APIs, ORM classes, user authentication, and library components.


โ“ Frequently Asked Questions (FAQs)

โ“ Can I access a private property in a subclass?
โŒ No. Use protected instead if it needs to be shared with subclasses.

โ“ Is it bad to make everything public?
โœ… Yes. It breaks encapsulation and makes your code harder to maintain or secure.

โ“ Can methods be private in PHP?
โœ… Absolutely. You can apply access modifiers to both properties and methods.

โ“ Should constructors be public or private?
โœ… Typically public, unless using patterns like Singleton or Factory where you want to restrict instantiation.

โ“ What happens if I omit a visibility modifier?
๐Ÿ”ธ In PHP, it defaults to public for methods (but you should always declare it explicitly for clarity).


Share Now :

Leave a Reply

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

Share

๐Ÿ” PHP Access Modifiers

Or Copy Link

CONTENTS
Scroll to Top