📜  PHP |访问说明符

📅  最后修改于: 2022-05-13 01:54:10.778000             🧑  作者: Mango

PHP |访问说明符

PHP,类的每个属性都必须具有三个可见性级别之一,称为publicprivateprotected

  • 公共:任何代码都可以访问公共属性,无论该代码是在类内部还是外部。如果属性被声明为 public,则可以从脚本中的任何位置读取或更改其值。
  • 私有:类的私有属性只能由类内部的代码访问。因此,如果我们创建一个声明为私有的属性,则只有同一类中的方法和对象才能访问其内容。
  • 受保护:受保护的类属性有点像私有属性,因为它们不能被类外的代码访问,但是从类继承的任何类都有一点不同,即基类也可以访问这些属性。

一般来说,尽可能避免创建公共属性是个好主意。相反,创建私有属性,然后创建允许类外的代码访问这些属性的方法更安全。这意味着我们可以准确地控制如何访问我们的类的属性。
注意:如果我们尝试访问类外的属性, PHP产生致命错误。
PHP访问说明符对类、子类和外部世界的可行性:

Class Member Access SpecifierAccess from own classAccessible from derived classAccessible by Object
PrivateYesNoNo
ProtectedYesYesNo
PublicYesYesYes

下面的例子说明了PHP的访问说明符:

  • 示例 1:在此示例中,我们将看到公共访问说明符。
php
x + $this->y ;
echo " ";
} 
    }    
class child extends GeeksForGeeks
{ 
function sub() 
{ 
echo $s = $this->x - $this->y ; 
} 
   
}   
 
$obj = new child; 
 
// It will return the addition result
$obj->add() ; 
 
// It's a derived class of the main class,
// which has a public object and therefore can be
// accessed, returning the subtracted result.
$obj->sub() ;
              
?>


php
a / $this->b ;
echo " ";
} 
    }    
class child extends GeeksForGeeks
{ 
function mul() 
{ 
echo $m = $this->a * $this->b ; 
} 
   
}   
 
$obj= new child; 
 
// It's supposed to return the division result
// but since the data and function are private
// they can't be accessed by a derived class
// which will lead to fatal error .
$obj->div();
 
// It's a derived class of the main class,
// which's accessing the private data
// which again will lead to fatal error .
$obj->mul();
?>


php
x / $this->y ;
echo " ";
}
    }    
class child extends GeeksForGeeks
{
function sub()
{
echo $s = $this->x - $this->y ;
}
 
}
class derived # Outside Class
{
function mul()
{
echo $m = $this->x * $this->y ;
}
     
}
$obj= new child;
 
// It will return the division result
$obj->div();
 
// Since it's a derived class of the main class,
$obj->sub();
 
// Since it's an outside class, therefore it
// will produce a fatal error .
$obj->mul();
 
?>


  • 输出:
150 50
  • 示例 2:在此示例中,我们将看到 Private Access Specifier

PHP

a / $this->b ;
echo " ";
} 
    }    
class child extends GeeksForGeeks
{ 
function mul() 
{ 
echo $m = $this->a * $this->b ; 
} 
   
}   
 
$obj= new child; 
 
// It's supposed to return the division result
// but since the data and function are private
// they can't be accessed by a derived class
// which will lead to fatal error .
$obj->div();
 
// It's a derived class of the main class,
// which's accessing the private data
// which again will lead to fatal error .
$obj->mul();
?> 
  • 输出:
PHP Fatal error:  Uncaught Error: Call to private method 
GeeksForGeeks::div() from context '' in /home/cg/root/8030907/
main.php:22 Stack trace:#0 {main} thrown in /home/cg/root/8030907/
main.php on line 22
  • 示例 3:在此示例中,我们将看到 Protected Access Specifier。

PHP

x / $this->y ;
echo " ";
}
    }    
class child extends GeeksForGeeks
{
function sub()
{
echo $s = $this->x - $this->y ;
}
 
}
class derived # Outside Class
{
function mul()
{
echo $m = $this->x * $this->y ;
}
     
}
$obj= new child;
 
// It will return the division result
$obj->div();
 
// Since it's a derived class of the main class,
$obj->sub();
 
// Since it's an outside class, therefore it
// will produce a fatal error .
$obj->mul();
 
?>
  • 输出 :
10
900
Fatal error:  Uncaught Error: Call to undefined method 
child::mul() in /home/cg/root/8030907/main.php:32