To control where and how class properties and methods can be accessed, use the following access control keyword:
Access Level | Description |
---|---|
private | allows access only to members of the same class. If A and B are instances of the class C, A can access the properties and methods of B. |
protected | allows access to members of the same class and instances from classes that inherit from that one only. |
public | accessible from anywhere. Any classes or code in general from outside the class can access it. |
<?php class Customer { private $id; private $firstname; private $surname; private $email; public function __construct( int $id, string $firstname, string $surname, string $email ) { $this->id = $id; $this->firstname = $firstname; $this->surname = $surname; $this->email = $email; } } $customer1 = new Customer(1, 'John', 'Doe', 'a@mail.com'); $customer2 = new Customer(2, 'Mary', 'Smith', 'mp@mail.com'); ?>