PHP protected

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;Code language: PHP (php)

As well as a method:

protected function methodName() 
{

   //...
}Code language: PHP (php)

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;
	}
}Code language: HTML, XML (xml)

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);
	}
}Code language: HTML, XML (xml)

Third, create an instance of the VIP class and call the getFormattedName() method:

<?php

$alex = new VIP('alex ferguson');
echo $alex->getFormattedName();Code language: HTML, XML (xml)

PHP issued the following error:

PHP Notice:  Undefined property: VIP::$name in ...Code language: PHP (php)

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;
	}
}Code language: HTML, XML (xml)

If you execute the script again, you’ll see the following in the output:

Alex Ferguson

PHP 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);
	}
}Code language: HTML, XML (xml)

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);
	}
}Code language: HTML, XML (xml)

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 AllenCode language: PHP (php)

Output:

 Bob Allen

In 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 FERGUSONCode language: PHP (php)

Output:

ALEX FERGUSON

This 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 protected properties and methods can only be accessed within the class and in any child classes of the class.
Did you find this tutorial useful?