PHP object-oriented private permission control

Source: Internet
Author: User
/*** ==== 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.
*/

?>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.