(Advanced) PHP Object-Oriented Learning (encapsulation, inheritance, and polymorphism)

Source: Internet
Author: User
Encapsulation is to encapsulate the extracted data and the operations on the data, and the data is protected internally. only authorized operations (methods) in other parts of the program can perform operations on the data. The following is the body of the article:


I. encapsulation

Encapsulation is to encapsulate the extracted data and the operations on the data, and the data is protected internally. only authorized operations (methods) in other parts of the program can perform operations on the data.

Php provides three access control modifiers

Public indicates global. this class can be accessed inside, outside, and subclass.

Protected indicates that it is protected and can only be accessed by this class or subclass.

Private indicates private, which can only be accessed within this class.

The above three modifiers can be used to modify both the method and the attribute (variable). if there is no access modifier for the method, the default value is public, and the access modifier must be specified for the member attribute, in PHP4, var $ name indicates public attributes, which is not recommended.

Example:

 Name = $ name; $ this-> age = $ age; $ this-> salary = $ salary;} public function showinfo () {// this indicates that all three modifiers can use echo $ this-> name. "| ". $ this-> age. "| ". $ this-> salary ;}}$ p1 = new Person ('Zhang San', 20,3000); // this is an external class, if the following method is used to access age and salary, an error is reported. // echo $ p1-> age; echo $ p1-> salary;?>


What should I do if I want to access the protected and private elements and methods externally?

The common practice is to access the variable formats through the public function:

public function setxxxx($val){     $this->xxxx=$val; } public function getxxxx(){     return $this->xxxx; }

Set and get are provided here for convenience of identification and are not necessary.

For example:

Public function getsalary () {return $ this-> salary; // extension: you can call some methods, such as determining the user name, to access the service correctly}

Echo $ p1-> getsalary ();

If you want to access protected and private, you can also use the following method, but it is not recommended to use it, as long as you know.

_ Set () and _ get ()

_ Set () assigns values to the protected or private attributes.

_ Set ($ name, $ val );

_ Get () gets the value of protected or private

_ Get ($ name );

For example:

 If pro_name is set, you cannot change $ this-> pro_name = $ pro_val;} // use _ get () to obtain all attribute values public function _ get ($ pro_name) {if (isset ($ pro_name) {return $ this-> pro_name;} else {return null ;}}$ n1 = new testa (); // normal condition, you cannot access the protected attribute outside the class, but you can use the above method to operate them $ n1-> name = 'small three'; echo $ n1-> name;?>


II. Inheritance

Let's take a look at an example:

 name.'||'.$this->age;     }     public function testing(){         echo 'this is pupil';     } } class Graduate{     public $name;     protected $age;     public function getinfo(){         echo $this->name.'||'.$this->age;     }     public function testing(){         echo 'this is Graduate';     } } ?>

As shown in the preceding example, when multiple classes have many common attributes and methods,

Low code reusability and code redundancy. think about the processing methods in css.

Solution: Inherit

 Name = $ name; $ this-> age = $ age;} public function showinfo () {echo $ this-> name. '| '. $ this-> age ;}} class Pupil extends Students {function testing () {echo 'pupil '. $ this-> name. is testing ';} class Graduate extends Students {function testing () {echo 'Graduate '. $ this-> name. is testing '; }}$ stu1 = new Pupil ('Zhang San', 20); $ stu1-> showinfo (); echo'
'; $ Stu1-> testing ();?>

As can be seen from the above, inheritance is a sub-class (Subclass) through The extends parent class

Continue the attributes and methods of public and protected in the parent class (BaseClass,

Private attributes and methods cannot be inherited.

Syntax structure:

Class parent class name {} class subclass name extends parent class name {}


Details:

1. a subclass can inherit only one parent class (here it refers to direct inheritance). If you want to inherit attributes and methods of multiple classes,

Multi-layer inheritance is supported.

Example:

 Name; // will output AAA here?>

2. when creating a subclass object, the constructor of its parent class is not automatically called by default.

Example:

Class A {public function _ construct () {echo 'A' ;}} class B extends A {public function _ construct () {echo 'B ';}} $ B = new B (); // The constructor in B is output first,

If there is no constructor in B

3. in the subclass, if you need to access the methods of the parent class (the constructor and member method modifier are protected or private ),

You can use the parent class: method name or parent: method name.

Here, both parent and self are in lower case and an error is reported in upper case]

Class A {public function test () {echo 'a _ test';} class B extends a {public function _ construct () {// you can use either of the following methods :: test (); parent: test () ;}$ B = new B ();

5. if the methods of a subclass (derived class) are exactly the same as those of the parent class (public, protected ),

We call it method override or method override. See the following polymorphism.

III. polymorphism

Example:

 Cry ();?>

Summary:

1. when a parent class knows that all child classes have a method, but the parent class cannot determine how to write the method, the child class can overwrite the method and overwrite the method ), the method names and parameters of subclass must be exactly the same.

2. if the subclass needs to call a method (protected/public) of the parent class, you can use the parent class name: method name or parent: method name.

3. access modifiers can be different during method rewriting, however, the access permission of the subclass method must be greater than or equal to that of the parent method (that is, the access permission of the parent method cannot be reduced)

For example, if the parent class public function cry () {} subclass protected function cry () {}, an error is returned.

However, the sub-class access permission can be enlarged, for example:

The parent class private function cry () {} subclass protected function cry () {} can be correctly executed.

The above is the content of PHP Object-Oriented Learning (encapsulation, inheritance, polymorphism). For more information, see PHP Chinese website (www.php1.cn )!

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.