类的封装修饰词:
1.public
#公开的
类外面可以访问,子类可以访问,父类可以访问.
类外面可以访问
<?php
// Person.class.php
class Person{
protected $name;
public $age;
public function __construct($n,$a){
$this->name=$n;
$this->age=$a;
}
public function say(){
echo "<p>my name is {$this->name}!</p>";
}
public function getAge(){
echo "<p>my age is {$this->age}!</p>";
}
}
class It extends Person{
public $programme;
public function __construct($n,$a,$p){
parent::__construct($n,$a);
$this->programme=$p;
}
public function develop(){
echo "my programme is {$this->programme}";
}
public function mysay(){
echo $this->name;
}
}
$obj=new It('user123',19,'吹水');
echo $obj->age;
?>
2.protected
#被保护的
类外面不能访问,子类可以访问,父类可以访问.
类外面不能访问
<?php
// Person.class.php
class Person{
protected $name;
public $age;
public function __construct($n,$a){
$this->name=$n;
$this->age=$a;
}
public function say(){
echo "<p>my name is {$this->name}!</p>";
}
public function getAge(){
echo "<p>my age is {$this->age}!</p>";
}
}
class It extends Person{
public $programme;
public function __construct($n,$a,$p){
parent::__construct($n,$a);
$this->programme=$p;
}
public function develop(){
echo "my programme is {$this->programme}";
}
}
$obj=new It('user123',19,'吹水');
echo $obj->name;
?>
子类可以访问
<?php
// Person.class.php
class Person{
protected $name;
public $age;
public function __construct($n,$a){
$this->name=$n;
$this->age=$a;
}
public function say(){
echo "<p>my name is {$this->name}!</p>";
}
public function getAge(){
echo "<p>my age is {$this->age}!</p>";
}
}
class It extends Person{
public $programme;
public function __construct($n,$a,$p){
parent::__construct($n,$a);
$this->programme=$p;
}
public function develop(){
echo "my programme is {$this->programme}";
}
public function mysay(){
echo $this->name;
}
}
$obj=new It('user123',19,'吹水');
echo $obj->mysay();
?>
3.private
#私有的
类外面不能访问,子类不能访问,父类可以访问.
私有的-
<?php
// Person.class.php
class Person{
protected $name;
private $age;
public function __construct($n,$a){
$this->name=$n;
$this->age=$a;
}
public function say(){
echo "<p>my name is {$this->name}!</p>";
}
public function getAge(){
echo "<p>my age is {$this->age}!</p>";
}
}
class It extends Person{
public $programme;
public function __construct($n,$a,$p){
parent::__construct($n,$a);
$this->programme=$p;
}
public function develop(){
echo "my programme is {$this->programme}";
}
public function mysay(){
echo $this->name;
}
}
$obj=new It('user123',19,'吹水');
echo $obj->age;
?>
类的封装级别:
子类的修饰词权限要<=父类的同名方法前的修饰词权限.
报级别错误
<?php
// Person.class.php
class Person{
protected $name;
private $age;
public function __construct($n,$a){
$this->name=$n;
$this->age=$a;
}
private function say(){
echo "<p>my name is {$this->name}!</p>";
}
public function getAge(){
echo "<p>my age is {$this->age}!</p>";
}
}
class It extends Person{
public $programme;
public function __construct($n,$a,$p){
parent::__construct($n,$a);
$this->programme=$p;
}
public function develop(){
echo "my programme is {$this->programme}";
}
private function say(){
echo $this->age;
}
}
$obj=new It('user123',19,'吹水');
echo $obj->say();
?>
**
本文详细介绍了PHP中类的封装概念,包括public、protected和private三种访问控制修饰符的使用方法及其权限范围,通过具体代码示例展示了不同修饰符下成员变量和方法的可访问性。

637

被折叠的 条评论
为什么被折叠?



