This article is a detailed analysis of the PHP object-oriented public private protected access modifiers, the need for a friend reference under
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
The 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 a private method"; }}//$w = new Woman (),//echo $w->info (),//echo $w->name;//Public properties can access//echo $w->age;//protected property, reported fatal error//echo $w height;//protected attribute, reported fatal error//private method, access error//$w->say (); Private method, access error class Girl extends woman{//can redefine the public and protected methods of the parent class, but cannot define private//protected $name = "Jingao";//Can New definition 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 here will not error echo "I am a Girl"; }} $g = new Girl (), $g->say (),//Normal output//echo $g->height;//Private property access is not output//$g->info ();//This is the output Gaojin22 $ The height is a private property that is not inherited//$g->height = "12";//Here is a redefinition of the height property is also assigned//$g->info ();//So this will output to gaojin2212