/***
====Notes Section====
Permission modifiers
Function: Used to illustrate the permission characteristics of attributes/methods
Write in front of properties/methods
There are 3 permission modifiers
private, the most strictly protected
protected
public , the least protected
Question:
Where can the properties/methods modified by public be accessed?
Where can private modified properties/methods be accessed?
How to determine whether a property/method has permission to access?
Answer: It depends on the location during the visit!
Private attributes/methods can only be accessed within the braces {} of the class definition
Public properties can be accessed from anywhere
***/
[php]
class human{
public $mood='';// Mood, public
private $money=500;//Money, private
public function getmoney(){
Return $this->money;
}
//Define private secret method
private function secret(){
echo 'I stole a piece of candy that day';
}
//Tell me your secret method
public function tellme(){
$this->secret();
}
}
$lisi=new human();
$lisi->mood='happay';
echo $lisi->mood,'
';//happay
echo $lisi->getmoney(),'
';//500
//echo $lisi->money=300;//The object cannot call private properties
//Fatal error: Cannot access private property human::$money in C:wampwwwphpprivate.php on line 31
//$lisi->secret();//Objects cannot call private methods
//Fatal error: Call to private method human::secret() from context '' in C:wampwwwphpprivate.php on line 32
$lisi->tellme(); // Yes, because it is called through line 17, that is, within the class.
/*
Summary: private permission control
Can only be called within {} of the class,
Once you get out of {}, no one can move you.
*/
?>
class human{
public $mood='';// Mood, public
private $money=500;//Money, private
public function getmoney(){
return $this->money;
}
//Define private secret method
private function secret(){
echo 'I stole a piece of candy that day';
}
//Tell me your secret method
public function tellme(){
$this->secret();
}
}
$lisi=new human();
$lisi->mood='happay';
echo $lisi->mood,'
';//happay
echo $lisi->getmoney(),'
';//500
//echo $lisi->money=300;//The object cannot call private properties
//Fatal error: Cannot access private property human::$money in C:wampwwwphpprivate.php on line 31
//$lisi->secret();//Objects cannot call private methods
//Fatal error: Call to private method human::secret() from context '' in C:wampwwwphpprivate.php on line 32
$lisi->tellme(); // Yes, because it is called through line 17, that is, within the class.
/*
Summary: private permission control
Can only be called within {} of the class,
Once you get out of {}, no one can move you.
*/
?>