当前位置:Gxlcms > PHP教程 > php面向对象访问控制public,private,protected详解_PHP教程

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

时间:2021-07-01 10:21:17 帮助过:6人阅读

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

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

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

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

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

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

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

那么我们可以这样认为:

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

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

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


实例

代码如下

error_reporting(E_ALL);

class test{

public $public;

private $private;

protected $protected;

static $instance;

public function __construct(){

$this->public = 'public
';

$this->private = 'private
';

$this->protected = 'protected
';

}

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
";

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
";

}

private function pri_function(){

echo "you request private function
";

}

}

$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 = 'public
'; $this->private = 'private
'; $this->protected = 'protected
'; } 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
"; 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
"; } private function pri_function(){ echo "you request private function
"; } } $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内部调用,实例化调用报错。

class test{

public $public;

private $private;

protected $protected;

static $instance;

public function __construct(){

$this->public = 'public
';

$this->private = 'private
';

$this->protected = 'protected
';

}

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
";

echo $this->public;

}

protected function pro_function(){

echo "you request protected function
";

echo $this->protected;

}

private function pri_function(){

echo "you request private function
";

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
";

}

public function pri_extends_function(){

echo "you request extends_private function
";

}

}

error_reporting(E_ALL);

$test = new test1();

$test -> tank(); //子类和父类有相同名字的属性和方法,实例化子类时,子类的中的属性和方法会盖掉父类的。

?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/629261.htmlTechArticle文章利用举例说明了关于PHP5面向对象访问控制 Public,private,protected详细说明,有需要的朋友可以参考一下。 在PHP5中增强了面向对象的机制,...

人气教程排行