$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();