/*** ==== Note ==== permission modifier: the permission features used to describe the attributes/methods are written in front of the attributes/methods. There are a total of three permission modifiers private. the most protected public protected by protected is the most loose question: attribute modified by public... "/> <scripttype =" text/javascript
/***
=== 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]
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 ,'
'; // Happay
Echo $ lisi-> getmoney (),'
'; // 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.
*/
?>
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 ,'
'; // Happay
Echo $ lisi-> getmoney (),'
'; // 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.
*/
?>