/***
=== Note ====
Permission Modifier
Role: describes the permissions of attributes and methods.
Before attribute/Method
There are 3 permission modifiers in total
Private, the strictest protection
Protected by protected
Public, the most loose Protection
Question:
Where can I access public modified attributes/methods?
Where can I access private modified attributes/methods?
How can I determine whether a property/method has access permissions?
A: Check the access location!
Private attributes/methods, which can be accessed only by {} in the braces defined by the class
Public attribute, which can be accessed anywhere
***/
[Php]
<? Php
Class human {
Public $ mood = ''; // mood, public
Private $ money = 500; // money, private
Public function getmoney (){
Return $ this-> money;
}
// Define the private secret method
Private function secret (){
Echo 'I stole a piece of sugar that day ';
}
// Tell me your secret method
Public function tellme (){
$ This-> secret ();
}
}
$ Lisi = new human ();
$ Lisi-> mood = 'happa ';
Echo $ lisi-> mood, '<br/>'; // happay
Echo $ lisi-> getmoney (), '<br/>'; // 500
// Echo $ lisi-> money = 300; // The object cannot call private attributes.
// Fatal error: Cannot access private property human: $ money in C: \ wamp \ www \ php \ private. php on line 31
// $ Lisi-> secret (); // The object cannot call private methods.
// Fatal error: Call to private method human: secret () from context ''in C: \ wamp \ www \ php \ private. php on line 32
$ Lisi-> tellme (); // yes, because it is called in the class through 17th rows.
/*
Conclusion: private permission Control
It can only be called within {} of a class,
Out of {}, no one can change.
*/
?>
<? Php
Class human {
Public $ mood = ''; // mood, public
Private $ money = 500; // money, private
Public function getmoney (){
Return $ this-> money;
}
// Define the private secret method
Private function secret (){
Echo 'I stole a piece of sugar that day ';
}
// Tell me your secret method
Public function tellme (){
$ This-> secret ();
}
}
$ Lisi = new human ();
$ Lisi-> mood = 'happa ';
Echo $ lisi-> mood, '<br/>'; // happay
Echo $ lisi-> getmoney (), '<br/>'; // 500
// Echo $ lisi-> money = 300; // The object cannot call private attributes.
// Fatal error: Cannot access private property human: $ money in C: \ wamp \ www \ php \ private. php on line 31
// $ Lisi-> secret (); // The object cannot call private methods.
// Fatal error: Call to private method human: secret () from context ''in C: \ wamp \ www \ php \ private. php on line 32
$ Lisi-> tellme (); // yes, because it is called in the class through 17th rows.
/*
Conclusion: private permission Control
It can only be called within {} of a class,
Out of {}, no one can change.
*/
?>