This article is the PHP object-oriented Public Private protected access modifier for a detailed analysis of the introduction, the need for friends under the reference
There are three types of access modifiers in PHP, respectively:
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.
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 of the class.
Graphic
Demo
Copy Code code as follows:
Class woman{
Public $name = "Gaojin";
protected $age = ";"
Private $height = "170";
function inf O () {
echo $this->name;
}
Private Function Say () {
echo "This is a proprietary method";
}
}
//$w = new Woman ();
//echo $w->info ()
//echo $w->name;//Public properties can access the
//echo $w->age;//protected Properties. Report fatal error
//echo $w->height;//protected Property, Report fatal error
//private method, access error
//$w->say ();//private method, access error
Class Girl extends Wom an{
//You can redefine the public and protected methods of the parent class, but you cannot define the private
//protected $name = "Jingao";//You can redefine
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 No output
//$g->info ();//This is output Gaojin22 $height is a private property that is not inherited
//$g->height = "12";//This is the redefinition of the Height property is also assigned
//$g->info ();// So it's going to output gaojin2212<./p>