Summary: in this tutorial, you will learn how to use the PHP protected access modifier to allow child classes to access properties and methods of the parent class.
Introduction to the PHP protected access modifier #
In the access modifier tutorial, you learned about the public and private access modifiers. The public properties and methods can be accessed from both inside and outside of the class while the private properties and methods can be accessed only within the class.
The protected properties and methods can be accessed from the inside of the class and any class that extends the class.
Like the private and public access modifier, you prefix the property name with the protected keyword to define a protected property:
protected $propertyName;As well as a method:
protected function methodName()
{
//...
}PHP protected property example #
First, define the Customer class that has a private property $name:
<?php
class Customer
{
private $name;
public function __construct($name)
{
$this->name = $name;
}
}Second, define the VIP class that extends the Customer class. The VIP class has a method getFormattedName() that accesses the $name property:
<?php
class VIP extends Customer
{
public function getFormattedName()
{
return ucwords($this->name);
}
}Third, create an instance of the VIP class and call the getFormattedName() method:
<?php
$alex = new VIP('alex ferguson');
echo $alex->getFormattedName();PHP issued the following error:
PHP Notice: Undefined property: VIP::$name in ...Since $name property is private, you can only access it within the Customer class. To allow the child class to access the $name property, you can change it to a protected property like this:
<?php
class Customer
{
protected $name;
public function __construct($name)
{
$this->name = $name;
}
}If you execute the script again, you’ll see the following in the output:
Alex FergusonPHP protected method example #
Let’s take an example of using the protected methods.
First, define the Customer class:
<?php
class Customer
{
protected $name;
public function __construct($name)
{
$this->name = $name;
}
protected function format()
{
return ucwords($this->name);
}
public function getName()
{
return $this->format($this->name);
}
}The Customer class has the protected property $name and protected method format(). The format() method returns the $name with the first character of each word converted to uppercase.
The Customer class also has the public method getName() that calls the format() method to return the formatted name.
Second, define the VIP class that extends the Customer class:
<?php
class VIP extends Customer
{
protected function format()
{
return strtoupper($this->name);
}
}The VIP class has the overriding format() method that returns the $name with all characters converted to uppercase. The format() method is also protected.
Third, create a new instance of the Customer class and display the name:
$bob = new Customer('bob allen');
echo $bob->getName(); // Bob AllenOutput:
Bob AllenIn this example, the getName() method invokes the format() method of the Customer class and returns the name with the first character of each word converted to uppercase.
Finally, create a new instance of the VIP class and show the name:
$alex = new VIP('alex ferguson');
echo $alex->getName(); // ALEX FERGUSONOutput:
ALEX FERGUSONThis time, the getName() method calls the format() of the VIP class instead of the Customer class. Therefore, you see the name is converted uppercase.
Summary #
- Use
protectedproperties and methods can only be accessed within the class and in any child classes of the class.
Thank you for your feedback!