protected
Posted 醉半仙
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了protected相关的知识,希望对你有一定的参考价值。
<?php
/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2017/1/21
* Time: 14:16
* public,private,protected的区别
*public:权限是最大的,可以内部调用,实例调用等。
*protected: 受保护类型,用于本类和继承类调用。
*private: 私有类型,只有在本类中使用。
*/
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
以上是关于protected的主要内容,如果未能解决你的问题,请参考以下文章
protected,private final 方法能否被代理