(Advanced article) PHP Object-oriented three characteristics of learning (encapsulation, inheritance, polymorphism)

Source: Internet
Author: User
Here is the text of the article:


First, the encapsulation of

Encapsulation is the extraction of data and the operation of the data is encapsulated together, the data is protected internally, the other parts of the program only authorized operations (methods) to manipulate the data.

PHP provides three types of access control modifiers

Public indicates global, inside of this class, outside of class, subclass can access

Protected represents protected, only this class or subclass can access

Private means that only this class can be accessed internally.

The above three modifiers can be decorated with either a method or a property (variable), and if there is no access modifier, the default is public, the member property must specify an access modifier, and in PHP4 the Var $name, which means that the property is exposed and is not recommended

Cases:

<?php class person{public     $name;     protected $age;     Private $salary;     function __construct ($name, $age, $salary) {         $this->name= $name;         $this->age= $age;         $this->salary= $salary;     }     Public Function Showinfo () {         ///This means that three modifiers can be used inside this class using         echo $this->name. $this->age. "| |". $this->salary;     } } $p 1=new person (' Zhang San ', 20,3000); This belongs to the outside of the class, then if you use the following method to access the age and salary will be an error//echo $p 1->age; echo$p1->salary;?>


So what should I do now to access the elements and methods of protected and private externally?

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

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

Here with set and get just for identification convenience, not necessary

Such as:

Public Function Getsalary () {     return $this->salary;     Extension: Here can call some methods, such as determining the user name, and so on to access}

Externally, Echo $p 1->getsalary () can be used.

If you want to access protected and private you can also use the following methods, but not recommended, as long as you understand

__set () and __get ()

__set () Assign a value to the protected or private property

__set ($name, $val);

__get () Gets the value of protected or private

__get ($name);

Such as:

<?php class testa{     protected $name;     Use __set () to manage all properties public     function __set ($pro _name, $pro _val) {         //above $pro_name and $pro_val can be customized         //$this-below >pro_name is established, cannot be changed         $this->pro_name= $pro _val;     }     Use __get () to get all property values public     function __get ($pro _name) {         if (isset ($pro _name)) {             return $this->pro_ name;         } else {             return null;}}     } $n 1=new testa ();//Normal, the protected property cannot be accessed outside the class, but it can be manipulated using the above method $n 1-> Name= ' small three '; echo $n 1->name;?>


Second, the inheritance of

Let's look at an example:

<?php class pupil{public     $name;     protected $age;     Public Function GetInfo () {         echo $this->name. ' | | '. $this->age;     }     Public Function testing () {         echo ' this was pupil ';     }} class graduate{public     $name;     protected $age;     Public Function GetInfo () {         echo $this->name. ' | | '. $this->age;     }     Public Function testing () {         echo ' this is graduate ';     }}?>

As you can see from the example above, when multiple classes have many common properties and methods,

Code reuse is not high, code redundancy, thinking about the processing methods in CSS

Workaround: Inherit

<?php class students{public     $name;     public $age;     Public function __construct ($name, $age) {         $this->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 Gr Aduate extends students{     function testing () {         echo ' graduate '. $this->name. ' is testing ';     }} $stu 1 = New pupil (' Zhang San ', 20); $stu 1->showinfo (); Echo ' <br/> '; $stu 1->testing ();?>

As can be seen from the above, inheritance is a subclass (subclass) by extends parent class

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

Cannot inherit private properties and methods

Syntax structure:

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


Details:

1, a subclass can inherit only one parent class (this refers directly to inheritance), if you want to inherit the properties and methods of multiple classes,

You can use multiple layers of inheritance

Cases:

<?php class a{public     $name = ' AAA ',} class B extends a{public     $age =30;} class C extends b{} $p =new C (); Echo $p->name;//here will output AAA?>

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

Cases:

Class a{public     function __construct () {         echo ' A ',     }} class B extends a{public     function __construct () {
  echo ' B ';     } } $b =new B ();//This will give precedence to the construction method in the output B,

If there is no construction method in B, it will output the

3. In subclasses, if you need to access the methods of the parent class (the constructor, the modifier for the member method method is protected or private),

Then you can use the parent class:: Method name or Parent:: Method name to complete

Here the parent and the previously mentioned self are both lowercase, uppercase error "

Class a{public     function test () {         echo ' a_test ',     }} class B extends a{public     function __construct () {
  
   //two kinds of methods are         a::test ();         Parent::test ();     } } $b =new B ();
  

5, if the method of a subclass (derived class) is exactly the same as the method of the parent class (public,protected),

We call method overrides or method overrides (override) to see the polymorphism below

Iii. polymorphism

Cases:

<?php class animal{public     $name;     public $price;     Function Cry () {         echo ' I don\ ' t know ';     }} class Dog extends animal{     //overwrite, override     function Cry () {         Echo ' Wang wang! ';         Animal::cry ();//There is no error, the cry () of the parent class can be executed correctly;     } } $dog 1=new Dog (); $dog 1->cry ();?>

Summary:

1, when a parent class knows that all subclasses have a method, but the parent class is not sure how to write the method, you can let the subclass to overwrite its methods, method overrides (Overrides), must require the subclass of the method name and the number of parameters exactly the same

2. If the subclass is going to call a method of the parent class (Protected/public), you can use the parent class Name:: Method Name or Parent:: Method Name

3. When implementing a method override, the access modifier can be different, but the access rights of the subclass method must be greater than or equal to the access rights of the parent method (that is, access to the parent method cannot be reduced)

If the parent class public Function Cry () {} Subclass protected function Cry () {} will error

However, the access rights of subclasses can be magnified, such as:

Parent class Private Function Cry () {} Subclass protected function Cry () {} can execute correctly

Above is (Advanced article) PHP Object-oriented three characteristics of learning (encapsulation, inheritance, polymorphism) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.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.