Php object-oriented encapsulation

Source: Internet
Author: User
Tags modifier php tutorial

<? Php Tutorial
/*
* Encapsulation: one of the three main characteristics of object-oriented
 *
* 1. Combine the members (attributes and methods) of an object into an independent unit and hide the internal details of the object as much as possible.
* Access permission modifier public protected private
* Private: private. Members modified with this keyword can only be accessed within the object (only accessed with $ this)
 * 
* Attributes can be encapsulated:
* If a variable needs to be used in multiple methods, the variable is declared as a member attribute and can be directly used in all methods of this object.
 *
* Member attribute, which is equivalent to the global variable in this object
 *
* All member attributes are used in the method. The change of member attribute values is actually changing the execution behavior of the method, that is, changing the object function.
 *
* If the value of the member attribute is abnormal, the function leaf of the method execution is abnormal.
 *    
* Function: You do not need to change or read its value outside the object.
* 1. Encapsulation
* Provide a public method (assign values and values to the object member attributes by using the method)
 *
 *
* Methods can also be encapsulated.
 *
* Function:
* 1. Use the private modifier so that it can only be used internally
 *
* 2. There are 100 methods in a class, and 95 methods are encapsulated (for the other 5 services). Only five methods can be used.
 *
* For attributes of 100 members, the value can be set and cannot be changed. Alternatively, the value can be changed only. The value cannot be obtained. // in this case, the following method is more convenient.
* Encapsulation-related magic methods:
 *
* _ Set (); // The method automatically called when the [private] member attribute value is directly set.
* _ Get (); // The method automatically called when the [private] member attribute value is directly obtained.
* _ Isset (); // This method is automatically called when you directly use isset () to check whether a private property exists in an object.
* _ Unset (); // The method automatically called when you directly use unset () to delete private attributes of an object.
 *
 *
 *
 *
 *
 *
 *
*/
Class Person {

// X encapsulates the member attributes and does not need to be changed outside the object
Private $ name;
Private $ age;
Private $ sex;


Private _ unset ($ proName ){
Unset ($ this-> $ proName );
  
  }


// This method is automatically called when you directly check whether a private attribute exists in an object.
// _ Isset ($ proName), $ proName indicates the attribute name
Private function _ isset ($ proName ){
Return isset ($ this-> $ proName); // isset () returns whether or not it exists
  }

Function _ construct ($ name, $ age, $ sex ){
$ This-> name = $ name;
$ This-> age = $ age;
$ This-> sex = $ sex;

  }

  
// This method is automatically called when private member attributes are obtained.
Private function _ get ($ proName)
  {
// Control the obtained value
If ($ proName = "age "){
If ($ this-age> 40)
Return $ this-> age-10;
   }
Return $ this-> $ proName;
  }

// This method is automatically called when private member attributes are set.
Private function _ set ($ proName, $ proValue ){
// $ ProName indicates the member attribute name, and $ proValue indicates the member attribute value
// Control the setting range
If ($ proName = "age "){
If ($ proValue> 100 | $ proValue <0)
Return;
   }
$ This-> $ proName = $ proValue;

   
  }

// Provides a public method to set the value of member attributes.
Function setAge ($ age ){
// Control the age range to increase security
If ($ age> 100 | $ age <0)
Return;
$ This-> age = $ age;
  }
  
// Provides a public method to obtain the attribute value of a member.
Function getAge (){
// Control the age range
If ($ this-> age <30)
Return $ this-> age;
Else if ($ this-> age <40)
Return $ this-> age-5;
Else if ($ this-> age <50)
Return $ this-> age;
Else
Return $ this-> age-15;
Provide public methods}

Function say (){
Echo "my name: {$ this-> name}, my age: {$ this-> age}, my surname: {$ this-> sex} <br> ";

// Access the encapsulated run () method
$ This-run ();
  }

Private function run (){
Echo '2017 <br>'

  }

Function eat (){

  }

// Destructor
Function _ destruct (){

  }
 }

$ P1 = new Person ("zhangsan", 25, "male ");
$ P2 = new Person;
$ P3 = new Person;
// $ P1-> age =-50; // because the age is randomly accessed from outside, the member attributes are encapsulated to ensure security.
$ P1-> setAge (30); // you can set the attribute value of a member.
$ P1-> getAge (); // Obtain the attribute value of a member using a method.

// You can directly call the member attributes by adding the magic method _ set ($ proName, $ proValue) _ get ($ proName).


$ P1-> say (); // call
$ P1-> run (); // private methods cannot be called directly

// Delete the name in $ p1
Unset ($ p1-> name );

// Determine whether the name exists
If (isset ($ p1-> name )){

Echo "exists <br> ";
} Else {
Echo "this member does not exist <br> ";
}


To:
Var $ name; // The name of the declarer
Var $ sex; // declare the gender of a person
Var $ age; // declare the person's age
Function run (){.......}
Changed to Encapsulation:
Private $ name; // encapsulate a person's name using the private keyword
Private $ sex; // encapsulate the gender of a person using the private keyword.
Private $ age; // encapsulate the age of a person using the private keyword.
Private function run (){......} // Encapsulate the human walking method with the private keyword
Note: as long as there are other keywords before the member attribute, the original keyword "var" should be removed ".
You can encapsulate a member (member attribute and member method) in private mode. The encapsulated members cannot be directly accessed outside the class, and only the objects can be accessed by themselves. The following code produces an error:
Class Person
{
// The following are the member attributes of a person.
Private $ name; // The name of a person, which is encapsulated by private
Private $ sex; // the gender of a person, which is encapsulated by private
Private $ age; // age of a person, which is encapsulated by private
// How this person can speak
Function say ()
{
Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age. "<br> ";
}
// The way this person can walk is encapsulated by private
Private function run ()
{
Echo "this person is walking ";
}
}
// Instantiate a person's instance object
$ P1 = new Person ();
// Try to assign values to private attributes, and the result will be incorrect
$ P1-> name = "James ";
$ P1-> sex = "male ";
$ P1-> age = 20;
// An error occurs when you try to print a private property.
Echo $ p1-> name. "<br> ";
Echo $ p1-> sex. "<br> ";
Echo $ p1-> age. "<br>"
// Try to print the private member method, and the result will be incorrect
$ P1-> run ();
Output result:
Fatal error: Cannot access private property Person: $ name
Fatal error: Cannot access private property Person: $ sex
Fatal error: Cannot access private property Person: $ age
Fatal error: Cannot access private property Person: $ name
Fatal error: Call to private method Person: run () from context''
From the instance above, we can see that private members cannot be accessed externally, because private members can only access the object themselves, for example, $ p1: the object wants to describe its private attributes and access the private attributes in the say () method. (No access control is added. The default value is public and can be accessed anywhere)

 

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.