Access Modifiers
In PHP, an access modifier is a keyword that you can put before a class's properties (its variables) or methods (its functions) to control where they can be accessed from. There are three types of access modifiers:
public
: This means that the property or method can be accessed from anywhere - both inside and outside of the class, and by objects of the class (an object is an instance of a class).private
: This means that the property or method can only be accessed from inside the same class. Not even the objects of the class can access it.
The last access modifier is protected
. This lesson is going to focus on public
or private
. A future lesson dedicated to the protected
modifier will come up.
Public modifier
The public
access modifier allows properties and methods of a class to be accessed from anywhere - both inside and outside of the class and by objects (instances) of the class.
Let's take a look at an example. Say we have a class Dog
, with a public property name and a public method bark:
class Dog {
public $name;
public function bark() {
echo "Woof! I'm " . $this->name . "!";
}
}
In this Dog
class, both the $name
property and the bark()
method are public. Access modifiers must always be positioned before the property or method names.
This means we can access them from outside the class. Let's create a Dog
object and use these public members:
$myDog = new Dog();
$myDog->name = "Fido"; // Set the name property
$myDog->bark(); // Call the bark method
When we run this code, it will output: "Woof! I'm Fido!"
Here we've created an object $myDog
from the Dog
class, and we've accessed the public property $name
and the public method bark
. The public
access modifier allowed us to do this from outside the Dog
class.
The public
access modifier is the most open one, and unless you have a specific reason to restrict access, you would typically make your methods and properties public.
Private modifier
The private
access modifier is used when a property or method should only be accessible from within the same class. This means you can't access them directly from outside the class or from an instance (object) of the class.
Let's look at an example. We'll create a class Cat
with a private property $name
and a private method meow()
:
class Cat {
private $name;
private function meow() {
echo "Meow! I'm " . $this->name . "!";
}
}
Now, if we try to access the private property or method from outside the class like this:
$myCat = new Cat();
$myCat->name = "Whiskers"; // Try to set the name property
$myCat->meow(); // Try to call the meow method
We will get an error. PHP won't allow us to access the name property or the meow method because they're private.
Getters and Setters
So, how can we use private properties and methods? There's a concept known as getters and setters. Getters and setters are special methods used to retrieve (get) or update (set) the value of a private or protected property of an object. These methods provide a way to control access to an object's properties.
We can create public methods in the same class to interact with them. Let's say we updated our Cat
class to the following:
class Cat {
private $name;
private function meow() {
echo "Meow! I'm " . $this->name . "!";
}
// A public method that sets the name
public function setName($name) {
$this->name = $name;
}
// A public method that calls the private method
public function makeSound() {
$this->meow();
}
}
$myCat = new Cat();
$myCat->setName("Whiskers"); // Set the name property through a public method
$myCat->makeSound(); // Call the private method through a public method
This will work fine, and it will output: "Meow! I'm Whiskers!"
Here, setName()
and makeSound()
are public methods, so we can call them from an object of the class. They can access the private property name and the private method meow because they're in the same class.
Why use the private modifier?
The private
access modifier has a few benefits:
- Control over Access: Private modifiers limit access to properties and methods of a class, preventing external code from directly changing the state of the object, which might cause unexpected behavior or bugs.
- Data Validation: By forcing external code to use setter methods to modify the state of an object, you can include validation logic in those methods. This can help ensure your object's state remains valid.
- Flexibility and Maintenance: By hiding the internal details of a class, you're free to change these details later without affecting the code that uses the class. As long as you keep the same methods and properties public, you can refactor the private parts freely.
- Reducing Side Effects: Using private properties helps prevent unintentional side effects that can occur if a class property was directly accessed or altered.
Key Takeaways
- In PHP, access modifiers include
public
,private
, andprotected
. They control where properties and methods can be accessed from. - The
public
modifier allows properties and methods to be accessed from anywhere - inside and outside of the class and from instances of the class. It is the most open access level. - The
private
modifier restricts access to properties and methods to the class that they're declared in. They can't be accessed from outside the class or from child classes. - Getters and setters are special methods used to read (get) or write (set) private and protected properties. They offer a controlled way to access and modify the state of an object, and they can include additional logic for data validation or formatting.