So far, all the properties and methods defined in our Book
class
were tagged as public
. That means that
they are accessible to anyone, or more precisely, from anywhere.
This is called the visibility of the property or method, and there are three
types of visibility. In the order of being more restrictive to
less, they are as follows:
private
: This type 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
: This type allows access to members of the same class and instances from classes that inherit from that one only. You will see inheritance in the next section.public
: This type refers to a property or method that is accessible from anywhere. Any classes or code in general from outside the class can access it.
In order to show some examples, let's first
create a second class in our application. Save this into a
Customer.php
file:
<?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; } }
This class represents a customer, and its
properties consist of the general information that the bookstores
usually know about their customers. But for security reasons, we
cannot let everybody know about the personal data of our customers,
so we set every property as private
.
So far, we have been adding the code to create objects in the same
Book.php
file, but since now we have two
classes, it seems natural to leave the classes in their respective
files, and create and play with objects in a separate file. Let's
name this third file init.php
. In order
to instantiate objects of a given class, PHP needs to know where
the class is. For that, just include the file with require_once
.
<?php require_once __DIR__ . '/Book.php'; require_once __DIR__ . '/Customer.php'; $book1 = new Book("1984", "George Orwell", 9785267006323, 12); $book2 = new Book("To Kill a Mockingbird", "Harper Lee", 9780061120084, 2); $customer1 = new Customer(1, 'John', 'Doe', 'johndoe@mail.com'); $customer2 = new Customer(2, 'Mary', 'Poppins', 'mp@mail.com');
You do not need to include the files every single time. Once you include them, PHP will know where to find the classes, even though your code is in a different file.
Note
Conventions for classes
When working with classes, you should know that there are some conventions that everyone tries to follow in order to ensure clean code which is easy to maintain. The most important ones are as follows:
- Each class should be in a file named the same
as the class along with the
.php
extension - Class names should be in CamelCase, that is, each word should start with an uppercase letter, followed by the rest of the word in lowercase
- A file should contain only the code of one class
- Inside a class, you should first place the properties, then the constructor, and finally, the rest of the methods
To show how visibility works, let's try the following code:
$book1->available = 2; // OK $customer1->id = 3; // Error!
We already know that the properties of the Book
class' objects are public, and therefore,
editable from outside. But when trying to change a value from
Customer
, PHP complains, as its
properties are private.