The difference between encapsulation inheritance polymorphism

Source: Internet
Author: User

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:

Copy CodeThe code is as follows:
<?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 internally within this class
echo $this->name. "| |". $this->age. "| |". $this->salary;
}
}
$p 1=new person (' Zhang San ', 20,3000);
This belongs to the outside of the class, so if you use the following method to access 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:

Copy CodeThe code is as follows:
<?php
Class testa{
protected $name;
Use __set () to manage all properties
Public Function __set ($pro _name, $pro _val) {
The above $pro_name and $pro_val can be customized
The following $this->pro_name are fixed and cannot be changed
$this->pro_name= $pro _val;
}
Use __get () to get all the property values
Public Function __get ($pro _name) {
if (Isset ($pro _name)) {
return $this->pro_name;
} else {
return null;
}
}
}
$n 1=new Testa ();
Normally, 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;
?>


The above code to understand the line, not recommended to use
second, the inheritance of
Let's look at an example:

Copy CodeThe code is as follows:
<?php
Class pupil{
Public $name;
protected $age;
Public Function GetInfo () {
echo $this->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 can be seen from the above example, when multiple classes have many common properties and methods, code reusability is not high, code redundancy, thinking about the processing methods in CSS
Workaround: Inherit

Copy CodeThe code is as follows:
<?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 Graduate 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) that inherits the properties and methods of public and protected in the parent class (BaseClass) through the extends parent class, and 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 to direct inheritance); If you want to inherit the properties and methods of multiple classes, you can use multi-level inheritance
Cases:

Copy CodeThe code is as follows:
<?php
Class a{
Public $name = ' AAA ';
}
Class B extends a{
Public $age = 30;
}
Class C extends b{}
$p =new C ();
echo $p->name;//will output AAA here
?>


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 B, if there is no construction method in B to output the
3. In subclasses, if you need to access a method of a parent class (a constructor method, a modifier for a member method method, protected or private), you can use the parent class: Either the method name or the parent:: Method name to complete "Both the parent and the previously mentioned self are lowercase, Uppercase Error "
Class a{
Public Function test () {
Echo ' A_test ';
}
}
Class B extends a{
Public Function __construct () {
Both of these methods are OK
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 are called method overrides or method overrides (override) to see the polymorphism below
Iii. Polymorphism
Cases:

Copy CodeThe code is as follows:
<?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 ();//This will not error, can correctly execute the parent class cry ();
}
}
$dog 1=new Dog ();
$dog 1->cry ();
?>

The difference between encapsulation inheritance polymorphism

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.