Home > Article > Backend Development > Detailed explanation of the three data access methods of PHP object-oriented (code examples)
Master the definition and function of the three methods of data access in PHP
1, public
2, protected
3, private
1,Public
: Public class members
can be accessed anywhere, specifically which ones can be accessed:
● The class that defines the class (self)
● Subclasses of this class
● Other classes
2, Protected
: Protected class members
● The class that defines this class ( Self)
● Subclasses of this class
3, Private
: Private class members
● Only self can access
Summary: From top to bottom, the ability to constrain is getting stronger and stronger
It is still a bit difficult for us to understand simply from the above text description, so we should still follow the theoretical and practical approach To understand, then we will use specific codes to understand how these three data access methods are implemented in PHP, what are their respective characteristics, and finally, let’s understand their binding capabilities. how.
Description: In order to allow everyone to better understand object-oriented, when writing articles, I also try to follow certain rules. From shallow to deep, from easy to difficult, so every article I write is related to the previous article. For example, in the following code case, I expand and extend the code of the previous article.
Case 1: Know the definition of public, and then prove that it can be accessed in 3 places (1. In a class defined by yourself, 2. In subclass 3. Outside)
<?php //定义 “人” 类 class Human{ public $name = "";//姓名 public $height = "";//身高 public $weight = "";//体重 public function eat($food){ echo $this->name."在吃".$food."<br/>"; } } //女主播 class Anchors extends Human{ public $name = ""; public $stageName = ""; public function __construct( $name,$stageName ){ $this->name = $name; $this->stageName = $stageName; } public function singing(){ echo "我是女主播,我会唱歌<br/>"; } } // Nba球员类 class NbaPlayer extends Human{ //因为父类已经有了,所以就不需要再写了,通过extends来实现 // public $name = "";//姓名 // public $height = "";//身高 // public $weight = "";//体重 public $team = "";//团队 public $playerName = "";//球员号码 public function __construct( $name,$height,$weight,$team,$playerName ){ $this->name = $name; $this->height=$height; $this->weight = $weight; $this->team = $team; $this->playName = $playerName; echo "构造函数执行了,当前对象是{$this->name}<br/>"; } //跑步 public function run(){ echo "跑步中<br/>"; } //跳跃 public function jump(){ echo "跳跃<br/>"; } //运球 public function dribble(){ echo "运球<br/>"; } //传球 public function pass(){ echo "传球<br/>"; } //投篮 public function shoot(){ echo "投篮<br/>"; } //扣篮 public function dunk(){ echo "扣篮<br/>"; } } //数据访问测试 //public 测试 //1、测试在类的外部可以访问 //创建乔丹对象 $jordon = new NbaPlayer("乔丹","1.98米","98公斤","公牛","23"); //输出乔丹对象 echo "名称= ".$jordon->name."<br/>";//结果能够输出来,说明在外部是可以访问Public类成员的 //2、测试在类的自身里面可以访问 //例子:比如构造函数里,我们可以直接通过$this->team //3、测试是否可以子类中可以访问 //例子:比如父类中定义的name 一样可以在子类(NbaPlayer)的构造函数中直接访问 ?>
The running effect is as follows:
Case Two: Know the definition of protected, and then prove that it can be accessed in two places (1. In the class defined by yourself, 2. In the subclass)
<?php //定义“人”类 class Human{ public $name = "";//姓名 public $height = "";//身高 public $weight = "";//体重 protected $addr = "长沙";//受保护的类 public function eat($food){ echo $this->name."在吃".$food."<br/>"; } //测试protected类成员是否可以在自身类中可以访问 public function getAddrInHuman(){ echo "Human 自身类中的add=".$this->addr."<br/>"; } } //女主播 class Anchors extends Human{ public $name = ""; public $stageName = ""; public function __construct( $name,$stageName ){ $this->name = $name; $this->stageName = $stageName; } public function singing(){ echo "我是女主播,我会唱歌<br/>"; } //重写方法eat public function eat($food){ echo "我是女主播,我是边唱歌边吃{$food}<br/>"; } } class NbaPlayer extends Human{ //因为父类已经有了,所以就不需要再写了,通过extends来实现 // public $name = "";//姓名 // public $height = "";//身高 // public $weight = "";//体重 public $team = "";//团队 public $playerName = "";//球员号码 public function __construct( $name,$height,$weight,$team,$playerName ){ $this->name = $name; $this->height=$height; $this->weight = $weight; $this->team = $team; $this->playName = $playerName; echo "构造函数执行了,当前对象是{$this->name}<br/>"; } //测试protected类成员是否可以在子类中可以访问 public function getAddrInNbaPlayer(){ echo "在NbaPlayer子类中addr=".$this->addr."<br/>"; } } //创建乔丹对象 $jordon = new NbaPlayer("乔丹","1.98米","98公斤","公牛","23"); //数据访问测试 //protected测试 //1、测试是否可以在自身类中可以访问 $jordon->getAddrInHuman();//结果OK,确实是可以访问 //2.测试是否可以在子类中访问 $jordon->getAddrInNbaPlayer();//结果OK,确实也是可以访问 //3.测试是否可以被外部访问 echo "在类外部访问addr=".$jordon->addr."<br/>";//结果报错,说明不行 ?>
The operation effect is as follows:
Case 3: Know the definition of private, and then prove that it can only be used in 1 place Access (1. In a self-defined class)
<?php //定义“人”类 class Human{ public $name = "";//姓名 public $height = "";//身高 public $weight = "";//体重 private $isHungry = true;//测试私有变量是否可以在子类中访问 //为了让外部可以访问private类成员 public function getPrivateInfo(){ return "private 变量 isHungry=".$this->isHungry."<br/>"; } } //女主播 class Anchors extends Human{ public $name = ""; public $stageName = ""; public function __construct( $name,$stageName ){ $this->name = $name; $this->stageName = $stageName; } public function singing(){ echo "我是女主播,我会唱歌<br/>"; } //重写方法eat public function eat($food){ echo "我是女主播,我是边唱歌边吃{$food}<br/>"; } } class NbaPlayer extends Human{ //因为父类已经有了,所以就不需要再写了,通过extends来实现 // public $name = "";//姓名 // public $height = "";//身高 // public $weight = "";//体重 public $team = "";//团队 public $playerName = "";//球员号码 public function __construct( $name,$height,$weight,$team,$playerName ){ $this->name = $name; $this->height=$height; $this->weight = $weight; $this->team = $team; $this->playName = $playerName; // echo "构造函数执行了,当前对象是{$this->name}<br/>"; } //测试private类成员是否可以在子类中访问 public function getIsHungryInNbaPlayer(){ echo "在NbaPlayer子类中isHungry=".$this->isHungry."<br/>"; } } //创建乔丹对象 $jordon = new NbaPlayer("乔丹","1.98米","98公斤","公牛","23"); //数据访问测试 //private测试 //1.测试私有变量能否被外部访问 echo "私有变量isHungry =".$jordon->isHungry ."<br/>";//结果不能访问,说明private变量不能被子类访问 //测试私有变量能否在定义的类的内部访问 echo $jordon->getPrivateInfo();//结果Ok,说明可以 //测试私有变量是否可以在子类中访问 $jordon->getIsHungryInNbaPlayer();//结果报错,说明也不能在子类中访问 ?>
The operation effect is as follows:
Summary:
1. Public: Public class members
can be accessed anywhere, specifically which ones can be accessed:
● The class that defines the class (self)
● Subclasses of this class
● Other classes
2. Protected: Protected class members
● Can be accessed by itself and its subclasses
3. Private: Private class members
● Only you can access them
4. In order to allow the outside world to access private class members, we can define a public class member in the class method, and then return the private class member in the public method
Okay, now that we are here, I think everyone should have a clearer understanding of data access, so let’s apply what we have learned now. Knowledge to solve the following questions, first think about how to do it, and then look at the answer
Question: NBA players generally don’t want others to know their real names. For example, I am obviously 40 years old, but I I just want others to think I am 38 years old
Thinking......................
The answer is revealed :
The code is as follows:
<?php class Human{ public $name = "";//姓名 public $height = "";//身高 public $weight = "";//体重 } //女主播 class Anchors extends Human{ public $name = ""; public $stageName = ""; public function __construct( $name,$stageName ){ $this->name = $name; $this->stageName = $stageName; } public function singing(){ echo "我是女主播,我会唱歌<br/>"; } //重写方法eat public function eat($food){ echo "我是女主播,我是边唱歌边吃{$food}<br/>"; } } class NbaPlayer extends Human{ //因为父类已经有了,所以就不需要再写了,通过extends来实现 // public $name = "";//姓名 // public $height = "";//身高 // public $weight = "";//体重 public $team = "";//团队 public $playerName = "";//球员号码 //数据访问 private $age = "40"; //end public function __construct( $name,$height,$weight,$team,$playerName ){ $this->name = $name; $this->height=$height; $this->weight = $weight; $this->team = $team; $this->playName = $playerName; echo "构造函数执行了,当前对象是{$this->name}<br/>"; } //跑步 public function run(){ echo "跑步中<br/>"; } //跳跃 public function jump(){ echo "跳跃<br/>"; } //运球 public function dribble(){ echo "运球<br/>"; } //传球 public function pass(){ echo "传球<br/>"; } //投篮 public function shoot(){ echo "投篮<br/>"; } //扣篮 public function dunk(){ echo "扣篮<br/>"; } //数据访问 public function getAge(){ // echo $this->name."的age=".$this->age."<br/>"; //既然能够在自身类中获取到年龄,那么为了解决NBA球员不想让别人知道自己真实年龄的问题 //我们就可以对年龄进行操作-造假 让年龄更小 $age = $this->age-2; echo $this->name."的age=".$age."<br/>"; } } //创建乔丹对象 $jordon = new NbaPlayer("乔丹","1.98米","98公斤","公牛","23"); $jordon->getAge();//结果Ok 说明年龄是可以获取到的,既然可以获取到age我们在函数内部作假,来达到一个谎报年龄的目的 ?>
The running effect is as follows:
##Summary:
1. Know that there are three forms of data access in PHP, public, protected, private2. Know the characteristics of the three data access methodsThe above is the detailed content of Detailed explanation of the three data access methods of PHP object-oriented (code examples). For more information, please follow other related articles on the PHP Chinese website!