Access Modifiers in PHP

PHPz
Release: 2024-08-29 13:06:31
Original
963 people have browsed it

Access modifier is a way to set accessibility scope and rights to the variable of any other PHP identifiers. PHP supports various keywords to make any variable to access any variable and the identifiers. We can assign these keywords to the class, function or identifiers. These keywords – public, private, protected, abstract, final, etc.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

When to use Access Modifiers in PHP?

PHP has some limitations on its access modifier, unlike Java. We cannot use all the PHP access modifier on the class level, function level, and identifier level. We can use these access modifiers as per our business need to grant permission or revoke permission throughout the program or the application.

Here are the list modifiers and whether it is applicable or not:

Access Modifier Class Level Function Level Variable Level
public NA YES YES
private NA YES YES
protected NA YES YES
abstract YES YES NA
final YES YES NA
Static NA YES YES

In the above tale, NA denotes the Not Applicable. That means we cannot use the public, private and protected on the class level. We can use the abstract and the final only on the class level.

Various Access Modifiers in PHP

Here are the following Access Modifiers in PHP mention below

1. Public access modifier

The public is the default modifier like JAVA in PHP. That means if we do not use any modifier with the functions of the identifiers by default, it is considered as a public access modifier. This is one of the most widely used. As the moment we come to the re-usability of the code of the function, we usually go with the public access modifier. Because the public can be used from anywhere, within the class for sure, outside of the class, in the extended class, and if that public re-usable is not bounded to any class, we can use that anywhere we include the file. As mentioned in the above table, we cannot use this public modifier with the class along with private and protected as well.

Now, it’s time to see the example of the public access modifier:

<?php
class MyAccess {
var $var = "This is first var";
// print var variable value
function returnVar() {
echo $this->var;
}
}
$obj1 = new MyAccess();$obj1->returnVar();
?>
Copy after login

In the above code, the returnVar() function has been defined with no access modifier, so this will work as public as this is the default modifier in the PHP language.

Access Modifiers in PHP

public, private and protected will not be applicable at the class level; let’s see it with an example.

<?php
class public MyAccess {
var $var = "This is first var";
function returnVar() {
echo $this->var;
}
}
$obj1 = new MyAccess();
$obj1->returnVar();
?>
Copy after login

The above code will give an error as mentioned below:

( ! ) Parse error: syntax error, unexpected ‘public’ (T_PUBLIC), expecting identifier (T_STRING) in E:wampwwwtwitindex.php on line 2

This remains the same for private and protected as well.

<?php
class private  MyAccess {
var $var = "This is first var";
}
?>
<?php
class protected  MyAccess {
var $var = "This is first var";
}
?>
Copy after login

2. Private access modifier

This modifier us the private keyword to process with it.  We cannot use the private modifier with the class. We can use this with the class variables and class methods only (as we have mentioned in the above table). When we declare and use the private, it cannot be accessed using the class’s object. It can only be used within the class.

For Example

<?php
class MyAccess {
var $var = "This is first var";
private $fist_name;
// simple class method
function returnVar() {
echo $this->fist_name;
}
function set_fist_name($set_this){
$this->fist_name = $set_this;
}
}
$obj1 = new MyAccess();
echo $obj1->fist_name; // will give the error
$obj1->set_fist_name("Jai Shre");
$obj1->returnVar();
?>
Copy after login

echo $obj1->fist_name; // will give the error

This line of code we can use as this will come up with the error. This is something we cannot access the private variable using the object of that class. But we can use this by using its setting and the getter method as we are using in the above code. $obj1->set_fist_name(“Jai Shre”); line of code will set the value in the variable and using $obj1->returnVar(); we can get the value of the set variable.

3. Protected access modifier

Just like public and private, protected itself doesn’t support at the class level. Like a private modifier, protected also restricts the class variables’ access or the function from outside of the class. It can be used within the same class and from the subclass (child class).

For Example 

<?php
class MyAccess {
var $var = "This is first var";
protected $fist_name;
// simple class method
function returnVar() {
echo $this->fist_name;
}
function set_fist_name($set_this){
$this->fist_name = $set_this;
}
}
class child extends MyAccess {
function setVal($set_this){
$this->fist_name = $set_this;
}
function getVal(){
echo $this->fist_name;
}
}
$obj1 = new child();
//echo $obj1->fist_name; // will give the error
$obj1->setVal("Jai Shre");
$obj1->getVal();
?>
Copy after login

echo $obj1->fist_name; the line of code will give the below error

Output:

Fatal error: Cannot access protected property MyAccess::$fist_name in E:wampwwwtwitindex.php on line 20

4. Abstract access modifier

It can be used on the class and the function, not on the class variable. If any class has at least one abstract function, then it must be declared as an abstract. We cannot instantiate the abstract class. An abstract class is mainly considered as an incomplete class.

5. Final access modifier

If any class is declared as a final, we cannot extend that class. PHP restricts the final class from being inherited.

6. Static access modifier

The static keyword can be used to make any function as static. It enables that function’s ability so that one can use it within creating an object of that class in which it has been declared. Static method example –

public static function static Function()
{
// declaration goes here..
}
Copy after login

Conclusion

We should always use the access modifier as per the business requirements. Using private and protected, we can restrict the direct use of private variables and private methods from outside of the declared class.

The above is the detailed content of Access Modifiers in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!