php 面向对象访问控制 public,private,protected详解
发布:smiling 来源: PHP粉丝网 添加日期:2014-03-31 23:02:44 浏览: 评论:0
文章利用举例说明了关于PHP5面向对象访问控制 Public,private,protected详细说明,有需要的朋友可以参考一下.
在PHP5中增强了面向对象的机制,加入了面向对象常见的public、private、protected这样的访问控制机制.从字面意思来理解:
Public 无疑是公共的意思,意思是说类本身和其外部的子类都可以访问这个属性或者方法;
Private 英文翻译过来就是私有的意思,只能是类本身在类的内部才能访问——实例化的对象句柄不能访问该属性和方法,子类也不能访问;
Protected 受保护的属性或者方法,这个属性或者是方法只能被类本身或者子类的内部访问,实例化的对象句柄不能访问.
现在我们举个例子来说这个问题.
假设有一个宝石博物馆的管理员 小李,把仓库中的宝石分为三个类别,红宝石,蓝宝石,绿宝石.并且划分了属性,红宝石是国家的任何人都可以参观标签为(public),蓝宝石管理员小李家族传下来的(protected),绿宝石是小李自己在山上见到的(private).
那么我们可以这样认为:
public标签的红宝石,是国家的——全民所有,只要是合法的国家的公民都能够参看和拍照.
Protected 这个是受保护的,只有小李家族的人(小李的孩子或者孙子)在特定的房间里面才能参观和拍照,不准拿到房间外边看——太小气了!
Private 这个是私有的,小李不想让其他人知道,更不想让拍照了,所以只能供小李在小李的房间里看看而已,其他的人即使小李的孩子们都没有办法看到
实例代码如下:
- <?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
- ?>
- <?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 ?>
- 从上面的例子中,我们可以看出,
- public: 可以class内部调用,可以实例化调用.
- private: 可以class内部调用,实例化调用报错.
- protected: 可以class内部调用,实例化调用报错.
- <?php
- 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>';
- }
- protected function tank(){ //私有方法不能继承,换成public,protected
- 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;
- }
- protected function pro_function(){
- echo "you request protected function<br>";
- echo $this->protected;
- }
- private function pri_function(){
- echo "you request private function<br>";
- echo $this->private;
- }
- }
- class test1 extends test{
- public function __construct(){
- parent::tank();
- parent::__construct();
- }
- public function tank(){
- echo $this->public;
- echo $this->private; //Notice: Undefined property: test1::$private
- echo $this->protected;
- $this->pub_function();
- $this->pro_function();
- $this->pri_function(); //Fatal error: Call to private method test::pri_function() from context 'test1'
- }
- public function pro_extends_function(){
- echo "you request extends_protected function<br>";
- }
- public function pri_extends_function(){
- echo "you request extends_private function<br>";
- }
- }
- error_reporting(E_ALL);
- $test = new test1();
- $test -> tank(); //子类和父类有相同名字的属性和方法,实例化子类时,子类的中的属性和方法会盖掉父类的.
- ?>
Tags: public private protected
- 上一篇:PHP面向对象概念与实例
- 下一篇:php的引用符号&用法
相关文章
- ·php5中public,private,protected 三种类属性的区别(2014-02-22)
- ·区别PHP中的const,static,public,private,protected(2020-02-22)
- ·PHP 5.0中多态性的实现方案浅析(2013-11-13)
- ·PHP 面向对象 继承(2013-11-14)
- ·PHP面向对象之private权限控制(2013-11-14)
- ·PHP 类属性 类静态变量的访问(2013-11-14)
- ·PHP面向对象开发——封装关键字(2013-11-14)
- ·php-对象的声名-构造-继承-案例(2013-11-14)
- ·开发大型PHP项目的方法(2013-11-27)
- ·__construct 构造函数用法(2013-11-28)
- ·什么是MVC模式(2013-11-28)
- ·php __get(),__set()函数来实现对属性的访问(2013-11-28)
- ·php $$是什么意思(2013-11-28)
- ·PHP魔术函数__autoload的用法与一些问题(2013-12-03)
- ·Fatal error: Cannot redeclare class 原因分析与解决办法(2013-12-06)
- ·几个PHP面向对象小实例(2013-12-08)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)