<?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