Dino Geek, try to help you

What is the difference between public private and protected in PHP?


In PHP, object-oriented programming (OOP) uses visibility keywords (`public`, `private`, and `protected`) to control access to class properties and methods. Understanding these access modifiers is essential for proper encapsulation and to ensure that your code follows best practices. Let’s delve into each keyword and provide examples to clarify their differences.

  1. Public

Public properties and methods are accessible from anywhere. This means you can access them from within the same class, from derived classes (subclasses), and even from instances of the class created outside the class structure.

```
class MyClass { public $publicProperty = “I can be accessed anywhere.”;

public function publicMethod() { echo “You can call me from anywhere.”; } }

$instance = new MyClass();
echo $instance->publicProperty; // Output: I can be accessed anywhere.
$instance->publicMethod(); // Output: You can call me from anywhere.
```

  1. Private

Private properties and methods are only accessible within the class that defines them. This means they are not accessible from derived classes (subclasses) or from outside the class.

```
class MyClass { private $privateProperty = “I can only be accessed within this class.”;

private function privateMethod() { echo “You can only call me within this class.”; } public function showPrivateProperty() { // We can access private property here echo $this->privateProperty; $this->privateMethod(); } }

$instance = new MyClass();
$instance->showPrivateProperty(); // Output: I can only be accessed within this class.You can only call me within this class.

// The following lines would cause errors:
// echo $instance->privateProperty;
// $instance->privateMethod();
```

  1. Protected

Protected properties and methods are similar to private ones in that they cannot be accessed from outside the class. However, they can be accessed by derived classes (subclasses).

```
class ParentClass { protected $protectedProperty = “I can be accessed within this class and by derived classes.”;

protected function protectedMethod() { echo “You can call me within this class and by derived classes.”; } }

class ChildClass extends ParentClass { public function showProtectedProperty() { // We can access protected property here echo $this->protectedProperty; $this->protectedMethod(); }
}

$instance = new ChildClass();
$instance->showProtectedProperty(); // Output: I can be accessed within this class and by derived classes.You can call me within this class and by derived classes.

// The following lines would cause errors:
// echo $instance->protectedProperty;
// $instance->protectedMethod();
```

  1. Summary

- Public: Accessible from anywhere (inside/outside the class and subclasses).
- Private: Accessible only within the class that defines it.
- Protected: Accessible within the class and any subclasses.

  1. Sources

1. PHP Manual – Visibility: The official PHP Manual provides detailed information on visibility keywords and examples. [PHP Manual: Visibility](https://www.php.net/manual/en/language.oop5.visibility.php)
2. W3Schools – PHP OOP Visibility: W3Schools offers a comprehensive guide to PHP OOP and visibility. [W3Schools: PHP OOP Visibility](https://www.w3schools.com/php/php_oop_access_modifiers.asp)

By understanding these access modifiers, you can better control the encapsulation and maintainability of your classes in PHP, ensuring your code is modular, secure, and easy to understand.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use