Access modifiers allows us to control the access, or visibility, of our properties. Here we will discuss about public and private access modifiers.

In the previous tutorial, we used the public and private access modifiers without any explanation. Mostly used access modifier is public access modifier. Here we will also learn about the private access modifier.

While the public access modifier allows a code to access from outside or inside of the class methods and properties. The private modifier prevents access to a class’s methods or properties from any code that is outside the class.

The Public Access Modifiers

You may be familiar with the following code. In the following code, the class property and methods names are defined as public, so that the code outside the class can be accessed directly.

//declare class
class Animal {

	//Properties
  public $family;

	//Methods
	public function getFamily(){
	    return 'I am from family ' . $this->family ;
	}
}

$cow = new Animal();
//Here we access a property from outside the class
$cow->family = 'Herbivore';
//Here we access a method from outside the class
echo $cow->getFamily();

Result:
I am from family Herbivore

The Private Access Modifiers

The class members with the Private Access Modifiers will be accessed within the class itself. It protects members from outside class access with the reference of the class instance.

In the below code we will set the property $family to private, and when we try to fetch the property value outside of the class we will get the error.

//declare class
class Animal {

	//Properties
  Private $family;

	//Methods
	public function getFamily(){
	    return 'I am from family ' . $this->family ;
	}
}

$cow = new Animal();
//Here we access a property from outside the class
$cow->family = 'Herbivore';
//Here we access a method from outside the class
echo $cow->getFamily();
Result:
Fatal error: Cannot access private property Animal::$family

How to access private property?

From above, we can see that the private property cannot be accessed outside of the class. But we need to display the property value. In order to interact with the private properties, we use public methods because they can interact with both the code outside of the class as well as inside of the class. The public methods that can interact in this manner are commonly divided into two kinds of methods:

Setters: To set the value of the public property
Getters: To get the value of the public property

In the following example, we will set and get the value of the private property $family, using the setter and getter methods. We will use the setFamily() methods to  set the value and getFamily() methods to get the value of the private property.

<?php
//declare class
class Animal {

	//Properties
  private $family;

  //Methods
	public function setFamily($family){
	    $this->family = $family;
	}


	//Methods
	public function getFamily(){
	    return 'I am from family ' . $this->family ;
	}
}

$cow = new Animal();
//Set the cow family
$cow->setFamily('Herbivore');
//Get the cow family
echo $cow->getFamily();


?>

Result:
I am from family Herbivore

Conclusion

In this tutorial, we have learned about private and public access modifiers. and how to get and set the values from private access modifiers.

If you have any questions, feel free to ask in the comments below .

If this tip has been helpful to you, please spend a second of your time and share it using the buttons below… thanks!

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *