<html> |
<head> |
<meta http-equiv= "Content-Type" content= "text/html; charset=gb2312" > |
<title> protected 关键字</title> |
</head> |
<body> |
<?php |
class Book{ |
protected $name = 'computer' ; //声明保护变量$name |
} |
class LBook extends Book{ //Book类的子类 |
public function showMe(){ |
echo '对于protected修饰的变量,在子类中可直接调用。例如:$name = ' . $this -> name; |
} |
} |
$lbook = new LBook(); //实例化对象 |
$lbook -> showMe(); |
echo '<p>但在其他的地方不可以调用,否则:' ; //对私有变量进行操作 |
$lbook -> name = 'history' ; |
?> |
</body> |
</html> |