There are three types of access modifiers in PHP, namely:
Public (common, default)
Protected (Protected)
Private (privately)
Public (common, default) in PHP5 if the class does not have an access modifier for the specified member, the default is the access permission for the publicly.
protected (protected) is declared as a member of the protected, allowing access only to subclasses of that class.
Private is defined as a member of private and is visible to all members within the class without access restrictions. Access is not allowed outside the class.
Graphic
Demo
Copy CodeThe code is as follows:
Class woman{
Public $name = "Gaojin";
protected $age = "22";
Private $height = "170";
function info () {
Echo $this->name;
}
Private function Say () {
echo "This is the private method";
}
}
$w = new Woman ();
echo $w->info ();
echo $w->name;//Public properties can be accessed
echo $w->age;//protected property, reported fatal error
echo $w->height;//protected property, reported fatal error
Private method, access error
$w->say (); Private method, access error
Class Girl extends woman{
You can redefine the public and protected methods of the parent class, but you cannot define the private
protected $name = "Jingao"; can be newly defined
function info () {
Echo $this->name;
Echo $this->age;
Echo $this->height;
}
function say () {
Parent::say ()///private method cannot be inherited if the say method of the parent class is protected there is no error.
echo "I am a Girl";
}
}
$g = new Girl ();
$g->say ();//Normal output
Echo $g->height;//Private Property access not output results
$g->info ();//This is the output gaojin22 $height is private property is not inherited
$g->height = "12";//Here is the redefinition of the Height property is also assigned
$g->info ();//So this will output to gaojin2212
http://www.bkjia.com/PHPjc/327990.html www.bkjia.com true http://www.bkjia.com/PHPjc/327990.html techarticle There are three types of access modifiers in PHP: Public (common, default) protected (protected) private (public, default) in PHP5 if the class does not have ...