First, the concept
Encapsulation is one of the three main features of object-oriented programming, which is to add the member property and the member method in the object to the access modifier, making it possible to hide the internal details of the object to achieve access control to the member ( remember not to deny access ).
Meaning:
A. Combining all member properties of an object with all member methods to form an indivisible independent unit (that is, an object)
B. Information concealment, that is, as far as possible to conceal the internal details of the object, the external formation of a boundary (or a barrier), only a limited external interface to make it contact with the outside.
Principle: It is required that other parts of the object cannot arbitrarily access the internal data of the object (member property and member method), so as to effectively avoid the "cross infection" of the external error, so that the software errors can be localized, greatly reducing the difficulty of error and troubleshooting.
PHP5 supports the following 3 types of access modifiers:
Public (default)
Private (privately)
Protected (Protected)
Second, set up private members
When declaring a member property or member method, using the Private keyword adornment implements a private encapsulation of the member. The encapsulated member cannot be accessed directly outside the object and can only be accessed using $this in the object's internal methods.
Since encapsulation is private and cannot be directly assigned to the outside of the object, and the value of the private property cannot be obtained directly outside the object, it is necessary to declare some common methods of manipulating private properties within the object if you do not want to do something to the private property outside of the object.
1<?PHP2 3 /**4 * Declare person this class5 */6 class Person7 {8 //Private member Properties9 Private $name;Ten Private $sex; One Private $age; A - function__construct ($name="",$sex= "Male",$age=1) - { the $this->name=$name; - $this->sex=$sex; - $this->age=$age; - } + - //This public method allows you to get the value of the private property $name outside the object + Public functionGetName () A { at return $this-name; - } - - //This public method allows you to assign a value to a private property outside the object, but restricts the condition - Public functionSetsex ($sex) - { in if($sex= = "Male" | |$sex= = "female") - { to $this->sex=$sex; + } - } the * //This public method allows you to assign a value to a private property outside the object, but restricts the condition $ Public functionSetage ($age)Panax Notoginseng { - if($age>150 | |$age<0) the return; + $this->age=$age; A } the + - Public functionSay () $ { $ Echo"My name is: {$this->name}, Gender: {$this->sex}, Age: {$this->age}<br> "; - } - } the - $zhangsan=NewPerson ("Zhang San", "male", 40);Wuyi the Echo $zhangsan->getname (). " <br> "; - $zhangsan->setsex ("female"); Wu $zhangsan->setage (200); - $zhangsan-say (); About $?>
In the above code, a person class is declared and all member properties are set to private properties, not directly accessible externally, but with permission access internally. Constructor method without keywords The default is public (the constructor does not set the permission to private), and the user can create the object by using a construction method and assign a value to the private property. Also provides a number of access interfaces that can access private member properties outside of an object.
Three, The Magic method
__set (): Used instead of a common set assignment method
format [modifier] function __set (string $name, mixed $value) {...} This method is called automatically when we assign a value directly to a non-public property in an object, and the property name takes the first argument (string), and the value as the second argument ( Mixed) is passed into this method.
__get (): Used to override the common get accessor method
Format: [modifier] Function __get (string $name) {...} This method is called automatically when we output a non-public property directly in an object, and the property name is passed in as the first argument.
1<?PHP2 3 class Person4 { 5 //The following is a member attribute of the declarator, all using the Private keyword encapsulation6 Private $name;//This property is encapsulated7 Private $sex;//This property is encapsulated8 //Declare Magic assignment method; $propertyName: member Property name, $propertyValue: Member property value9 Public function__set ($propertyName,$propertyValue) Ten { One $this-$propertyName=$propertyValue; A } - //declaring Magic value method; $propertyName: member Property name - Public function__get ($propertyName) the { - return $this-$propertyName; - } - } + $person 1=NewPerson (); - $person 1->name = "John Doe";//the __set () method is called automatically to assign a value to the private property name + $person 1->sex = "female";//the __set () method is automatically called for private property sex assignment A at Echo $person 1->name;//Automatic Call Magic Method __get () Gets the property name value - Echo $person 1->sex;//automatically Call Magic method __get () Get Property sex value - -?>
__isset (): detects the existence of a member property in an object
Format: [modifier] Function __isset (string $name) {...} This method is called automatically when you use Isset () or empty () to determine whether a private or protected property exists for an object. The parameter name indicates the name of the property being judged.
__unset (): Destroying member property methods in an object
Format: [modifier] Function __unset (string $name) {...} When you use Unset () to destroy a private or protected property of an object, this method is called automatically and the property name is passed in as the first argument.
1<?PHP2 classPerson {3 Private $name;//This property is encapsulated4 Private $sex;//This property is encapsulated5 Public function__construct ($name="",$sex= "Male") {6 $this->name =$name; 7 $this->sex =$sex;8 }9 Public function__isset ($propertyName)//requires a parameter that is the name of the measured private propertyTen { One return isset($this-$propertyName);//other properties can be measured and returned to the measured results. A } - Public function__unset ($propertyName)//requires a parameter that is the name of the private property to be deleted - { the if($propertyName= = "Name")//if the property name passed in in the parameter equals "name", the condition is set - return;//exit method, do not allow deletion of the Name property in the object - unset($this-$propertyName);//Delete the private property specified outside the object within the object - } + } - $person 1=NewPerson ("Zhang San", "male");//create an Object $person1, assign the member property to the initial value separately + Var_dump(isset($person 1->name));//output bool (true), does not allow the determination of the Name property A Var_dump(isset($person 1->sex));//output bool (true), there is a sex private property at Var_dump(isset($person 1->id));//output bool (FALSE), the id attribute does not exist in the measurement object - unset($person 1->name);//Delete private property name, but not allowed in __unset () - unset($person 1->sex);//Delete private property in object sex, delete succeeded - - -?>
Note: The above four magic methods are valid only for private, protected member properties in a class. The modifier before the Magic method can be public, private, and does not affect the invocation.
PHP Object-oriented (ii)