OOP encapsulation
The three main features of object-oriented are encapsulation , inheritance , polymorphism
The encapsulation of OOP
hides the field and implementation details of the object, only exposes the interface externally, controls the access level of the Read and modified fields in the program, and combines the data and the behavior (or function) of the abstraction to form an organic whole, that is, to combine the data with the source code of the operational data to form a "class", Where the data and functions are members of the class. Scope of field:1. Public public (can be accessed outside of the class) 2. Private (can be accessed within the Class)3. protected protected (within-class and subclass-accessible, out-of-class access) what is within a class, that is, the range within the curly brackets that creates the class is called within the class, and other places are called outside the class . The creation uses a private field so that the external cannot access the
Example
classcomputer{//the field (member) of the class//private is privatized, that is, the operation that encapsulates the field, cannot be accessed outside the class, and cannot be manipulated by values and assignmentsPrivate $_name= ' Lenovo ';Private $_mode= ' I7 ';}$computer 1=Newcomputer ();//cannot be accessed at this timeEcho $computer 1-_name;#access private fields using public methods as portals, and must use the $this keyword. classcomputer{//Field (member) of the classPrivate $_name= "Lenovo";//In this case, an external public method is used to access the private field//Because the private field can only be accessed within the class, while the external public method is within the class//and the public method is public, so it can be accessed outside the class. Public function_run () {//when a field is called within a class, it must be a class-and-field, and $_name is just a normal variable. The method that the field calls outside the class is an object---field, and the class must use computer->_name//but in this class, you can use a keyword instead of computer, which is $thisEcho $this-_name;}}$computer 1=Newcomputer ();Echo $computer 1->_run ();
Assigning and taking values for private fields
You can design two public methods, one of which is SetName (), which is used to assign values, and one method is GetName (), which is used to take values.
classcomputer{//Field (member) of the class Private $_name= "Lenovo";//Construction Method Public function__construct () {Echo"<meta charset=utf8>"; }//Assign Value Public functionSetName ($name){//The $_name here is just a variable, the argument is//this->_name is the field of the class $this->_name=$name;}//Take value Public functionGetName () {return $this-_name; }}$computer 1=Newcomputer ();$computer 1->setname ("Dell");Echo $computer 1->getname ();
Interception device
If there are 10 fields then there must be 20 methods to be able to assign values and values, PHP built-in two methods (interceptors) dedicated to the value and assignment: __set (), __get ()
classcomputer{Private $_name ; Private $_cpu; Private $_model; Public function__construct () {Echo"<meta charset=utf8>"; } //when an object outside the class calls the private field directly, it will then go to check the Interceptor,//If the $_name is directly assigned, then the __set () method will intercept, will not error//Use the Interceptor Assignment and value//assignment Public function__set ($name,$value) { //If $name = ' _name ' if $value= "Lenovo"//$this->_name= ' Lenovo '//If $name= ' _cpu ' if $value= ' 4 core '//$this-&G T;_CPU = ' 4 Core '//If $name= ' _model ', if $value= ' i7 '//$this->_model= ' i7 ' $this-$name=$value; } //Take value Public function__get ($name) { //If $key = ' _name ' then $this->_name//if $key = ' _cpu ' then $this->_cpu//if $key = ' _model ' then $this->_m Odel return $this-$name; }}$computer 1=Newcomputer ();$computer 1->_name = "Lenovo";$computer 1->_model = "i7";$computer 1->_CPU = "4 Cores";Echo $computer 1-_name;Echo $computer 1-_model;Echo $computer 1->_cpu;
tip : Privatize member methods or call outside the class, which is why?
PHP Object-oriented features (i)