PHP Object-oriented Programming (ii), PHP object-oriented Programming _php tutorial

Source: Internet
Author: User

PHP Object-oriented Programming (ii), PHP object-oriented programming


Now let's look at an object-oriented encapsulation problem

Encapsulation: In my understanding can be understood as a U-disk we use the interface of the U-disk and the computer for the interaction between the data but we can not see the structure of the inside this feature we may call encapsulation

Advantage: With this feature we can maximize the quality of the code we use in other code as long as the interface is not written every time to increase the amount of code and reduce the difficulty of eliminating bugs

Now let's think of a problem: A personal computer has a password and doesn't want anyone else to log in and copy and paste it. There is a human object, height and age attributes, can only be to increase their own, can not let others arbitrarily assigned to the value and so on.

We use the keyword private to encapsulate the code

private $name;//The person's name using the Private keyword to encapsulate the private $sex;//The person's gender using the private keyword to encapsulate the private $age; Use the Private keyword to encapsulate a person's age private function run () {...}//Take a person's way of walking using the Private keyword for encapsulation

Note: As long as there are other keywords in front of the member properties, you should remove Var

 
  Name. "Gender:". $this->sex. "My Age is:". $this->age;} This person can walk the way, was the private package on a private function run () {echo "This person is walking";}} Instantiate an Instance object $p1 = new Person ();//try to assign a value to a private property, the result will be error $p1->name = "Zhang San"; $p 1->sex = "male"; $p 1->age = 20;//trying to print private property, the result will be an error echo $p 1->name;echo $p 1->sex;echo $p 1->age;//attempt to print the private member method, the result will be error $p1->run (); >

  

The output is:

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 '
Without any access control, the default is public and can be accessed anywhere.

This person can speak in a way that speaks his own private property, and here also can access the Private method function say () {echo "My name is called:". $this->name. "Gender:". $this->sex. "My Age is:". $this->age;//can also access private methods here//$this->run ();}

Because the Member method say () is public, it is possible to call the Say () method outside of the class to change the above code:

 
  The name assigns the value $this->name = $name;//The $sex to the private member property $this->sex the value $this->sex = $sex by means of the constructor method; The age gives the private member property $this->age the initial value $this->age = $age;} This person can speak in a way that speaks his own private property, and here also can access the Private method function say () {echo "My name is called:". $this->name. "Gender:". $this->sex. "My Age is:". $this->age;}} The construction method creates 3 objects $p1, P2, $p 3, each passing in three different arguments for name, gender, and age $p1 = new Person ("Zhang San", "male"), $p 2 = new Person ("John Doe", "female", "3"), $p = new Pe Rson ("Harry", "male", 40);//The following access to the $p1 object in the speech method $p1->say ();//The following Access $p2 object in the speech method $p2->say ();//Access $p3 object in the following way $p3->say () ;? >

  Because the constructor method is the default public method (the construction method is not set to private), if the setting is called private, it is equivalent to sealing the only interface in the U disk, we cannot access the class

As we can see in the above example, private members can only be used inside the class, not directly from outside the class, but sometimes we need to assign and read private properties, and we need to provide some accessible interfaces to the outside of the class.

Prvate $age; The private attribute Age function setage ($age)//provides a public setting age for the external method {if ($age <0 | | $age >130)//When assigning a value to an attribute, in order to avoid an illegal value set to the property return;$ This->age = $age;} function getage ()//provides a method for the external public acquisition Age {return ($this->age);}

Let's take a look at the application of __set,__get,__isset,__unset four methods

After the above explanation may have the question then how do we operate on the private class?????

__set Settings: __get

 
  $property _name) {return ($this, $property _name);} Else{return (NULL);}} The __set () method is used to set the private property function __set ($property _name, $value) {$this, $property _name = $value;}

A complete example:

 
  if (Isset ($this, $property _name)) {return ($this, $property _name);} Else{return NULL;}} The __set () method is used to set the private property function __set ($property _name, $value) {echo] automatically calls this __set () method to assign a value to a private property when the value of the private property is set directly
"; $this $property _name = $value;}} $p 1 = new person ();//The operation that assigns a value directly to a private property automatically calls the __set () method for assignment $p1->name = "Zhang San"; $p 1->sex = "male"; $p 1->age = 20;//Get the value of the private property directly , the __get () method is called automatically, returning the value of the member property echo "Name:". $p 1->name. "
"; echo" Gender: ". $p 1->sex. "
"; echo" Age: ". $p 1->age. "
";? >

  

Program execution Results:

The __set () method is automatically called when a private property value is set directly to assign a value to a private property
The __set () method is automatically called when a private property value is set directly to assign a value to a private property
The __set () method is automatically called when a private property value is set directly to assign a value to a private property
This __get () method is called automatically when a private property value is obtained directly
Name: Zhang San
This __get () method is called automatically when a private property value is obtained directly
Gender: Male
This __get () method is called automatically when a private property value is obtained directly
Age: 20

If the above code does not add the __get () and __set () methods, the program will go wrong because the private members cannot be manipulated outside the class, and the above code is automatically called by the __get () and __set () methods to help us directly access the encapsulated private members.

If you use the "isset ()" function outside an object to determine if the members of the object are set, can you use it? In two cases, if the members of the object are public, we can use this function to determine the member properties, if it is a private member property, this function will not work, because the private is encapsulated, is not visible externally. Then we can not use the "isset ()" function outside the object to determine whether the private member property is set? Yes, you can just add a "__isset ()" method to the class, and when you use the "isset ()" function outside of the class to determine whether a private member of the object is set, the "__isset ()" Method inside the class is automatically invoked to help us do this, "__ The Isset () "method can also be made private. You can add the following code to the class:

Private Function __isset ($NM) {echo] automatically calls when a private member $nm is measured outside the class using the Isset () function
"; return Isset ($this-$nm);}

Complete example

 
  $property _name) {return ($this, $property _name);} Else{return NULL;}} The __set () method is used to set the private property __set ($property _name, $value) {$this, $property _name = $value;} __isset () method The Private function __isset ($nm) {echo ' isset () function automatically calls
"; return Isset ($this-$nm);} __unset () method Private Function __unset ($NM) {echo] is automatically called when a private member is removed by using the unset () function outside the class.
"; unset ($this, $nm);}} $p 1 = new person (), $p 1->name = "The is a person name", or//when the Isset () function is used to measure a private member, the __isset () method is automatically called to help us complete, returning the result to Trueecho Var_d UMP (Isset ($p 1->name)). "
"Echo $p 1->name."
";///When you use the unset () function to delete a private member, the __unset () method is automatically called to help us complete, delete the name private property unset ($p 1->name);//has been deleted, this line will not have output echo $p 1->name; >

  

The output is:

The Isset () function automatically calls when a private member is measured
Boolean true
This was a person name
Automatically called when a private member is removed by using the unset () function outside the class.
The Isset () function automatically calls when a private member is measured

Here's what we're going to learn about inherited knowledge.

Here's the human code.

Define a "person" class as the parent class person{//below is the member property of the person var $name;//person's name Var $sex;//gender var $age;//person's age//define a constructor method parameter for property name $name, sex $ Sex and Age $age assignment function __construct ($name, $sex, $age) {$this->name = $name; $this->sex = $sex; $this->age = $age;} This person can speak in a way that speaks his own attribute function say () {echo "My name is called:". $this->name. "Gender:". $this->sex. "My Age is:". $this->age;}}

Here is the code for the Student class

Class student{//Below is a member of the person attribute var $name;//person's name Var $sex;//gender var $age;//person's age var $school;//Student's School attribute//Define a constructor method parameter is a property name $nam E, gender $sex and age $age assignment function __construct ($name = "", $sex = "", $age = "", $school = "") {$this->name = $name; $this-> ; sex = $sex; $this->age = $age; $this->school = $school;} This person can speak in a way that speaks his own attribute function say () {echo "My name is:". $this->name. "Gender:". $this->sex. "My Age is:". $this->age. "
";} This student learns the method function study () {echo "My name is:". $this->name. "I am." $this->school. "Learning
";}}

Simplifying the class of students

Class Student extends Person{var $school;//The properties of the student's school//The way the student Learns function study () {echo "My name is:". $this->name. "I am." $this->school. "Learning
";}}

Now we have to consider the problem of overloading.

This time you may have doubts PHP seems to be unable to overload, because PHP is a language, the traditional overload is that there are multiple methods to name the same method, but with a different number of arguments, we use different parameters to call different interfaces

The overload I'm talking about is a subclass that overrides the parent class

 
  name = $name; $this->sex = $sex; $this->age = $age;} This person can speak in a way that speaks his own attribute function say () {echo "My name is called:". $this->name. "Gender:". $this->sex. "My Age is:". $this->age;}} Class Student extends Person{var $school;//Student's School properties//The way the student Learns function study () {echo "My name is called:". $this->name. "I am." $this->school. "Learning";} This learning can be spoken in a way that says all of its properties, overriding the parent class with the same name method function say () {echo "My name is called:". $this->name. "Gender:". $this->sex. "My Age is:". $this->age. "I am in". $this->school. "Go to School";}? >

That's the way it's going to work. The main points are:

1. Child class to inherit from parent class

2. The method name of the subclass is the same as the method name of the parent class

At this point, we may find that if there are 1000 code in this method, it is inconvenient to implement it.

At this point, we use the "Parent:: Method name" side test to invoke the overridden method in the parent class;

Class Student extends Person{var $school;//The properties of the student's school//The way the student Learns function study () {echo "My name is called:". $this->name. "I am." $this->school. "Learning";} This learning can speak the way, say all of their properties, overriding the parent class of the same name method function say () {//using the parent class "class name::" To invoke the overridden method in the parent class;//Person::say ()//or using "Parent::" To invoke the overridden method in the parent class; Parent::say ();//Add a bit of your own function echo "My age is:". $this->age. "I am in". $this->school. "Go to School";}}

The difference between the public,private,protected

 
  Mypublic (); $this->myprotected (); $this->myprivate ();}} $myclass = new MyClass; $myclass->mypublic ();//works$myclass->myprotected ();//Fatal error$myclass-> Myprivate ();//Fatal Error$myclass->foo ();//Public, Protected and Private work/** * Define MyClass2 */class MyClass2 E Xtends myclass{//This is Publicfunction Foo2 () {$this->mypublic (); $this->myprotected (); $this->myprivate () ;//Fatal Error}} $myclass 2 = new MyClass2; $myclass 2->mypublic ();//Works$myclass2->foo2 ();//Public and Protected Work, not private?>

From the above code we can summarize the following

Public: External access can be made directly

Protected indirect external access requires an interface like a USB flash drive and a computer, and that interface requires a subclass (subclasses inherit the protected of the parent class)

Private cannot be accessed externally

Since we're talking about inheritance, If we want a class that's not inherited then we can use final to define it (only classes and methods can be defined and member properties cannot be defined)

1.final tagged classes cannot be inherited

2.final tagged methods cannot be overridden by a quilt class

 
  

  

The following error will appear:

Fatal Error:class Student may not inherit from final Class (person)
 
  

Understanding the use of key keywords for static and const (self:)

Static literal meaning is statically meaning now you might ask what are the benefits of static use of static? The advantage of using static is that if there are a number of "people" objects in the example, there is a common attribute such as nationality "China", then we can build a nationality this property is set to static, within the existence of a location, in the process of instantiation, thousands of people will access this location in memory

Static members can restrict external access because static members belong to the class, are not part of any object instance, are allocated space when the class is loaded for the first time, other classes are inaccessible, only instances of the class are shared, and the members of the class can be protected to a certain extent;

This is a bit like a global variable in a Web site

 
  

The result is:

China I am a Chinese American

You can write it like that.

 
  Showconstant ();//echo $class:: constant; It's not allowed,?>.

Member properties that are decorated with "const" are accessed in much the same way as members of the "static" adornment, using the "self" keyword in the method, as well as using the "Class name". However, you do not have to use the "$" symbol and you cannot use objects to access it.

 
  Showconstant ();//echo $class:: constant; It's not allowed,?>.

  

http://www.bkjia.com/PHPjc/1101501.html www.bkjia.com true http://www.bkjia.com/PHPjc/1101501.html techarticle PHP Object-oriented Programming (ii), PHP object-oriented programming now let's look at an object-oriented encapsulation of the problem encapsulation: in my understanding can be understood as a U-disk we ...

  • 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.