PHP Object-oriented – encapsulation

Source: Internet
Author: User

Packaging:
Encapsulation, which is the encapsulation of objective things into abstract classes, and classes can put their own data and methods only trusted class or object operation, to the untrusted information hiding.

The above operation is to rely on the following access control symbols to achieve, set their own permissions, so that some people can access, and some people can not access

Routines:

[PHP]View PlainCopy 
  1. <span style="FONT-SIZE:18PX;" ><?php
  2. Class person{
  3. public $name;
  4. private $age;
  5. private $salary;
  6. function __construct ($name,$age,$salary) {
  7. $this->name=$name;
  8. $this->age=$age;
  9. $this->salary=$salary;
  10. }
  11. }
  12. $a 1=new person ("xiaoming", 32,5000);
  13. Correct display, no restrictions on global property access
  14. echo "<br>". $a 1->name.  "<br>";
  15. Error, private property cannot be accessed outside the class
  16. echo "<br>" $a 1->age. " <br> ";
  17. Error, private property cannot be accessed outside the class
  18. echo "<br>" $a 1->salary. " <br> ";
  19. ?></span>


PHP provides three types of access control symbols to control access to methods and properties (variables).

1. Public indicates global, can be used in this class, outside of class, subclass
2. Protected is protected, can be used in this class, subclass, cannot use n outside the class
3. Private means proprietary, can only be used in this class, cannot be used outside the class, that is, new in other places, and cannot call the method


These three control modifier symbols can be decorated with attributes and methods
1. If a method does not have an access modifier, the default is public

2. The attribute must specify an access modifier, and if there is no access modifier, an error is given.

3. The private method is best preceded by an underscore, such as: Private Function _save () {}


Methods can be called to each other. But you need to use $this reference
function test11 ()
{
$this->test12 ();
}
protected function test12 ()
{
echo "test12 Hello";
}

To illustrate:

[PHP]View PlainCopy 
  1. <?php
  2. Class person1{
  3. public $name;
  4. protected $age;
  5. private $salary;
  6. function __construct ($name,$age,$salary) {
  7. $this->name=$name;
  8. $this->age=$age;
  9. $this->salary=$salary;
  10. }
  11. //Member Method
  12. Public function Showinfo () {
  13. //Here is a description of the variables that can be modified in this class using public protected and private.
  14. echo $this->name."| |". $this->age. "||".  $this->salary;
  15. }
  16. function Test () {
  17. //Call the Showinfo () method and use this reference
  18. $this->showinfo ();
  19. }
  20. }
  21. Create a person
  22. $p 1=New Person1 ("Monkey King", 30,1000);
  23. $p 1->test ();
  24. ?>

To access the protected variable or private variable outside of the class, it is common practice to create a public method in the class to access the variables. form is
Public Function setxxx ($val) {
}
Public Function getXxx () {
}
Public Function Showinfo () {
}

To illustrate:

[PHP]View PlainCopy  
    1. <?php
    2. Class person2{
    3. public $name;
    4. protected $age;
    5. private $salary;
    6. function __construct ($name,$age,$salary) {
    7. $this->name=$name;
    8. $this->age=$age;
    9. $this->salary=$salary;
    10. }
    11. //Member Method
    12. Public function Showinfo () {
    13. //Here is a description of the variables that can be modified in this class using public protected and private.
    14. echo "<br>". $this->name. "||". $this->age. "||".  $this->salary;
    15. }
    16. }
    17. Create a person
    18. $p 1=New Person2 ("Pig", 20,5000);
    19. Access to its details, as the attributes age and salary are protected and private variables, if accessed directly outside of the class
    20. Its properties are error-able, but a function that creates a public property in a class accesses these variables through a function.
    21. $p 1->showinfo ();
    22. ?>

PHP Object-oriented – encapsulation

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.