Summary: in this tutorial, you will learn about PHP access modifiers, including public and private, and understand the differences between them.
Introduction to PHP access modifiers
In the objects and classes tutorial, you have learned about how to use the public
access modifier with properties and methods.
PHP has three access modifiers: public
, private
, and protected
. In this tutorial, you’ll focus on the public
and private
access modifiers.
- The
public
access modifier allows you to access properties and methods from both inside and outside of the class. - The
private
access modifier prevents you from accessing properties and methods from the outside of the class.
The public access modifier
When you place the public
keyword in front of a property or a method, the property or method becomes public. It means that you can access the property and method from both inside and outside of the class.
The following example illustrates the public
access modifier:
<?php
class Customer
{
public $name;
public function getName()
{
return $this->name;
}
}
Code language: HTML, XML (xml)
In this example, the Customer
class has a public
property ($name
) and public
method (getName()
). And you can access the property and method from both inside and outside of the class. For example:
<?php
class Customer
{
public $name;
public function getName()
{
return $this->name;
}
}
$customer = new Customer();
$customer->name = 'Bob';
echo $customer->getName(); // Bob
Code language: HTML, XML (xml)
How it works.
- First, create a new instance of the
Customer
class. - Second, set the value of the
$name
property to'Bob'
. - Third, call the
getName()
method of the$customer
object and display the name.
The private access modifier
To prevent access to properties and methods from outside of the class, you use the private
access modifier.
The following example changes $name
property of the Customer
class from public
to private
:
<?php
class Customer
{
private $name;
public function getName()
{
return $this->name;
}
}
Code language: HTML, XML (xml)
If you attempt to access the $name
property from the outside of the class, you’ll get an error. For example:
$customer = new Customer();
$customer->name = 'Bob';
Code language: PHP (php)
Error:
Fatal error: Uncaught Error: Cannot access private property Customer::$name
Code language: PHP (php)
So how do you access a private
property?
To manipulate the value of a private property, you need to define a public method and use the method to manage a private property.
Typically, you need to define two kinds of public methods to manage a private property:
- A getter returns the value of the private property.
- A setter sets a new value for the private property.
By convention, the getter and setter have the following name:
getPropertyName
setPropertyName
The following Customer
class defines the getter (getName
) and setter(setName)
to get and set the value of the $name
property:
<?php
class Customer
{
private $name;
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
}
Code language: HTML, XML (xml)
And the following shows how to use the setName()
and getName()
methods to set and get the value of the $name
property:
$customer = new Customer();
$customer->setName('Bob');
echo $customer->getName();
Code language: PHP (php)
Why do you need private property?
It may be faster to use the public
access modifier for properties instead of using a private property with the public getter/setter.
However, by using the private
property, you can prevent direct access to the property from the outside of the class.
In addition, the getter/setter methods ensure that the only way to access the property is through these methods. And the getter/setter methods can provide custom logic to manipulate the property value.
For example, if you want the value of the $name
property to be not blank, you can add the validation logic to the setName()
method as follows:
<?php
class Customer
{
private $name;
public function setName($name)
{
$name = trim($name);
if ($name == '') {
return false;
}
$this->name = $name;
return true;
}
public function getName()
{
return $this->name;
}
}
$customer = new Customer();
$customer->setName(' Bob ');
echo $customer->getName();
Code language: HTML, XML (xml)
In the setName()
method:
- First, remove all leading and trailing whitespace using the
trim()
function. - Second, return false if the
$name
argument is blank. Otherwise, assign it to the$name
property and returntrue
.
Summary
- Use the
public
access modifier to allow access to properties and methods from both inside and outside of the class. - Use the
private
access modifier to prevent access from the outside of the class. - Do use
private
properties with a pair of public getter/setter methods.