Home  >  Article  >  Backend Development  >  A simple comparison of public, private and protected in PHP

A simple comparison of public, private and protected in PHP

青灯夜游
青灯夜游Original
2019-02-01 17:20:434704browse

There are three access modifiers in PHP: public, private and protected. You can define the visibility of properties, methods or constants by adding these keywords before the declaration. So what's the difference between them? Here is a brief introduction in this article, I hope it will be helpful to everyone.

A simple comparison of public, private and protected in PHP

PHP public access modifier

The public modifier can be used both internally and externally. If a class member is declared public, it can be accessed from anywhere. [Video tutorial recommendation: PHP tutorial]

Example:

<?php 
header("content-type:text/html;charset=utf-8");  
// BaseClass 
class pub { 
    public $tag_line = "php中文网!"; 
    function display() { 
        echo $this->tag_line."<br/>"; 
    } 
} 
  
// 子类
class child extends pub { 
    function show(){ 
        echo $this->tag_line; 
    } 
}  
  
// 对象声明
$obj= new child; 
  
// 输出
echo $obj->tag_line."<br/>";  
  
$obj->display();  
  
$obj->show();  
?>

Output:

A simple comparison of public, private and protected in PHP

private access modifier

The private modifier can be used in the class in which it is defined and its parent or inherited classes. If a class member is declared protected, it can only be accessed within the class itself and from inheritance and parent classes.

Example:

<?php 
header("content-type:text/html;charset=utf-8");  
// 基类
class pro { 
    protected $x = 500; 
    protected $y = 500; 
              
    // 实现减法 
    function sub()  
    { 
        echo $sum=$this->x-$this->y . "<br/>"; 
    }      
}  
  
// 子类-继承类
class child extends pro { 
    function mul() //实现乘法
    { 
        echo $sub=$this->x*$this->y; 
    } 
}  
  
$obj= new child; 
$obj->sub(); 
$obj->mul(); 
?>

Output:

A simple comparison of public, private and protected in PHP

protected access modification The

#protected modifier can be used in the class in which it is defined. Note: It cannot be accessed outside the class means inherited classes.

If a class member is declared private, it can only be accessed by the class in which the member is defined.

Example:

<?php 
header("content-type:text/html;charset=utf-8");  
// 基类
class demo { 
    private $name="PHP中文网!"; 
      
    private function show() 
    { 
        echo "这是基类的私有方法"; 
    } 
}  
  
// 子类
class child extends demo { 
    function display() 
    { 
        echo $this->name; 
    } 
}  
  
// 对象声明
$obj= new child; 
  
// 出现异常---未捕获错误:调用私有方法demo::show()
//$obj->show();  
  
//出现异常--未定义的属性:子级::$name
$obj->display(); 
?>

Output

A simple comparison of public, private and protected in PHP:

Explanation:

As you can see from the above example, it will show an error because private class data cannot be accessed outside the class.

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of A simple comparison of public, private and protected in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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