There are three access modifiers in PHP, namely:
public (public, default)
protected (protected)
private (private)
public (public, default) In PHP5, if a class does not specify an access modifier for a member, the default is public access.
protected (Protected) Members declared as protected are only allowed to be accessed by subclasses of this class.
private (private) Members defined as private are visible to all members within the class and have no access restrictions. Access is not allowed outside the class.
Illustration
demo
Copy code Code As follows:
class Woman{
public $name = "gaojin";
protected $age = "22";
private $height = "170";
Function info () {
Echo $ this-& gt; name;
}
Private Function say () {
Echo "This is a private method";
}
}
//$w = new Woman();
//echo $w->info();
//echo $w->name;//Public properties can be accessed
/ /echo $w->age;// Protected attribute, report fatal error
//echo $w->height;// Protected attribute, report 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 private methods
//protected $name = "jingao"; // You can redefine
function info(){
echo $this->name;
echo $this->age;
echo $this->height;
}
function say(){
//parent::say();//Private methods cannot be inherited If the parent class's say method is protected here No error will be reported
echo "I am a girl";
}
}
$g = new Girl();
$g->say();//Normal output
//echo $g->height;//The private attribute cannot be accessed and no result is output
//$g->info();//This is the output gaojin22 $height is a private attribute and has not been Inherit
//$g->height ="12";//The height attribute is redefined and assigned a value
//$g->info();//So gaojin2212 will be output here
http://www.bkjia.com/PHPjc/327990.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327990.htmlTechArticleThere are three access modifiers in PHP, namely: public (public, default) protected (protected) private (private) public (public, default) in PHP5 if the class does not...