php 面向对象访问控制 public,private,protected详解

文章利用举例说明了关于PHP5面向对象访问控制 Public,private,protected详细说明,有需要的朋友可以参考一下.

在PHP5中增强了面向对象的机制,加入了面向对象常见的public、private、protected这样的访问控制机制.从字面意思来理解:

Public 无疑是公共的意思,意思是说类本身和其外部的子类都可以访问这个属性或者方法;

Private 英文翻译过来就是私有的意思,只能是类本身在类的内部才能访问——实例化的对象句柄不能访问该属性和方法,子类也不能访问;

Protected 受保护的属性或者方法,这个属性或者是方法只能被类本身或者子类的内部访问,实例化的对象句柄不能访问.

现在我们举个例子来说这个问题.

假设有一个宝石博物馆的管理员 小李,把仓库中的宝石分为三个类别,红宝石,蓝宝石,绿宝石.并且划分了属性,红宝石是国家的任何人都可以参观标签为(public),蓝宝石管理员小李家族传下来的(protected),绿宝石是小李自己在山上见到的(private).

那么我们可以这样认为:

public标签的红宝石,是国家的——全民所有,只要是合法的国家的公民都能够参看和拍照.

Protected 这个是受保护的,只有小李家族的人(小李的孩子或者孙子)在特定的房间里面才能参观和拍照,不准拿到房间外边看——太小气了!

Private 这个是私有的,小李不想让其他人知道,更不想让拍照了,所以只能供小李在小李的房间里看看而已,其他的人即使小李的孩子们都没有办法看到

实例代码如下:

  1. <?php
  2. error_reporting(E_ALL);
  3. class test{
  4. public $public;
  5. private $private;
  6. protected $protected;
  7. static $instance;
  8. public function __construct(){
  9. $this->public = 'public <br>';
  10. $this->private = 'private <br>';
  11. $this->protected = 'protected <br>';
  12. }
  13. static function tank(){
  14. if (!isset(self::$instance[get_class()]))
  15. {
  16. $c = get_class();
  17. self::$instance = new $c;
  18. }
  19. return self::$instance;
  20. }
  21. public function pub_function() {
  22. echo "you request public function<br>";
  23. echo $this->public;
  24. echo $this->private; //private,内部可以调用
  25. echo $this->protected; //protected,内部可以调用
  26. $this->pri_function(); //private方法,内部可以调用
  27. $this->pro_function(); //protected方法,内部可以调用
  28. }
  29. protected function pro_function(){
  30. echo "you request protected function<br>";
  31. }
  32. private function pri_function(){
  33. echo "you request private function<br>";
  34. }
  35. }
  36. $test = test::tank();
  37. echo $test->public;
  38. echo $test->private; //Fatal error: Cannot access private property test::$private
  39. echo $test->protected; //Fatal error: Cannot access protected property test::$protected
  40. $test->pub_function();
  41. $test->pro_function(); //Fatal error: Call to protected method test::pro_function() from context
  42. $test->pri_function(); //Fatal error: Call to private method test::pri_function() from context
  43. ?>
  44. <?php error_reporting(E_ALL); class test{ public $public; private $private; protected $protected; static $instance; public function __construct(){ $this->public = 'public <br>'; $this->private = 'private <br>'; $this->protected = 'protected <br>'; } static function tank(){ if (!isset(self::$instance[get_class()])) { $c = get_class(); self::$instance = new $c; } return self::$instance; } public function pub_function() { echo "you request public function<br>"; echo $this->public; echo $this->private; //private,内部可以调用 echo $this->protected; //protected,内部可以调用 $this->pri_function(); //private方法,内部可以调用 $this->pro_function(); //protected方法,内部可以调用 } protected function pro_function(){ echo "you request protected function<br>"; } private function pri_function(){ echo "you request private function<br>"; } } $test = test::tank(); echo $test->public; echo $test->private; //Fatal error: Cannot access private property test::$private echo $test->protected; //Fatal error: Cannot access protected property test::$protected $test->pub_function(); $test->pro_function(); //Fatal error: Call to protected method test::pro_function() from context $test->pri_function(); //Fatal error: Call to private method test::pri_function() from context ?>
  45. 从上面的例子中,我们可以看出,
  46. public: 可以class内部调用,可以实例化调用.
  47. private: 可以class内部调用,实例化调用报错.
  48. protected: 可以class内部调用,实例化调用报错.
  49. <?php
  50. class test{
  51. public $public;
  52. private $private;
  53. protected $protected;
  54. static $instance;
  55. public function __construct(){
  56. $this->public = 'public <br>';
  57. $this->private = 'private <br>';
  58. $this->protected = 'protected <br>';
  59. }
  60. protected function tank(){ //私有方法不能继承,换成public,protected
  61. if (!isset(self::$instance[get_class()]))
  62. {
  63. $c = get_class();
  64. self::$instance = new $c;
  65. }
  66. return self::$instance;
  67. }
  68. public function pub_function() {
  69. echo "you request public function<br>";
  70. echo $this->public;
  71. }
  72. protected function pro_function(){
  73. echo "you request protected function<br>";
  74. echo $this->protected;
  75. }
  76. private function pri_function(){
  77. echo "you request private function<br>";
  78. echo $this->private;
  79. }
  80. }
  81. class test1 extends test{
  82. public function __construct(){
  83. parent::tank();
  84. parent::__construct();
  85. }
  86. public function tank(){
  87. echo $this->public;
  88. echo $this->private; //Notice: Undefined property: test1::$private
  89. echo $this->protected;
  90. $this->pub_function();
  91. $this->pro_function();
  92. $this->pri_function(); //Fatal error: Call to private method test::pri_function() from context 'test1'
  93. }
  94. public function pro_extends_function(){
  95. echo "you request extends_protected function<br>";
  96. }
  97. public function pri_extends_function(){
  98. echo "you request extends_private function<br>";
  99. }
  100. }
  101. error_reporting(E_ALL);
  102. $test = new test1();
  103. $test -> tank(); //子类和父类有相同名字的属性和方法,实例化子类时,子类的中的属性和方法会盖掉父类的.
  104. ?>