PHP three-object-oriented learning (full understanding of abstraction, encapsulation, inheritance, polymorphism) _php skills

Source: Internet
Author: User
Tags inheritance modifier modifiers
The three main features of facial alignment: encapsulation, inheritance, polymorphism, first of all, a simple understanding of the abstract:
When we define a class in the first place, we actually extract the attributes and behaviors that are common to a class of things and form a physical model (template), a method called abstract

first, the encapsulation of
Encapsulation is the encapsulation of the extracted data and the operation of the data, the data is protected internally, and other parts of the program are authorized operations (methods) to manipulate the data.
PHP provides three types of access control modifiers
Public represents global, within this class, outside of the class, subclasses can access
Protected represents a protected, only this class or subclass can access the
Private is privately represented and only within this class can be accessed
The above three modifiers can either decorate the method or modify the property (variable), and the method is public by default if there is no access modifier, and the member property must specify an access modifier, which is also in the PHP4 var $name, which indicates that the property is exposed and is not recommended.
Cases:
Copy Code code 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 (' John ', 20,3000);
This belongs to the outside of the class, so if you access age and salary in the following way, you will get an error
Echo $p 1->age; echo$p1->salary;
?>

So what do you want to do now to access protected and private elements and methods 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 only for easy identification, not necessary
Such as:
Public Function getsalary () {
return $this->salary; Extension: Here you can call some methods, such as judging the user name, and so on, right to access
}
1->getsalary () can be used externally through the echo $p;
If you want to access protected and private you can also use the following methods, but do not recommend use, as long as you know
__set () and __get ()
__set () assignment to protected or private properties
__set ($name, $val);
__get () Gets the value of protected or private
__get ($name);
Such as:
Copy Code code as follows:

<?php
Class testa{
protected $name;
Use __set () to manage all properties
Public Function __set ($pro _name, $pro _val) {
$pro_name and $pro_val above can be customized
The following $this->pro_name is established and 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 ();
Normally, the protected property cannot be accessed outside of a class, but it can be manipulated using the above method
$n 1->name= ' San Xiao ';
Echo $n 1->name;
?>

The above code understands on line, does not recommend to use
second, the succession of
Let's look at one example:
Copy Code code 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 Code code 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 (' John ', 20);
$stu 1->showinfo ();
Echo ' <br/> ';
$stu 1->testing ();
?>

As you can see from the above, inheritance is a subclass (subclass) that inherits the public and protected properties and methods of the parent class (BaseClass) by extends the 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 only inherit a parent class (this refers to direct inheritance), and if you want to inherit the properties and methods of multiple classes, you can use a multilevel inheritance
Cases:
Copy Code code 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 you create a subclass object, 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 the parent class's method (the constructor, the member method method's modifier is protected or private), you can use the parent class:: Method name or Parent:: Method name to complete "here parent and previously mentioned self are all lowercase, Uppercase Error "
Class a{
Public Function test () {
Echo ' A_test ';
}
}
Class B extends a{
Public Function __construct () {
Both 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), see the following polymorphism
Three, polymorphism
Cases:
Copy Code code 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 ()//here will not be an error, the correct implementation of the parent class cry ();
}
}
$dog 1=new Dog ();
$dog 1->cry ();
?>

Summary:
1, when a parent class knows all of the subclasses have a method, but the parent class is not sure how to write the method, you can let subclasses to override its methods, method overrides (Overrides), must require a subclass of the method name and 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. The access modifiers can be different when the implementation method overrides, but the access permission for the subclass method must be greater than or equal to the access rights of the parent class method (that is, the access rights of the parent method cannot be reduced)
such as the parent class public Function Cry () {} Subclass protected function Cry () {} will cause an error
However, the access rights of subclasses can be magnified, such as:
Parent class Private Function Cry () {} Subclass protected function Cry () {} can execute correctly
Extended:
Method overload (Overload)
Basic concept: The function name is the same, but the number of parameters or the type of parameters are different to call the same function, you can distinguish between different functions
Overloading is also supported in PHP5, but it is quite different from other languages, and multiple functions with the same name cannot be defined in PHP
PHP5 provides a powerful "magic" function, using these magic functions, we can do function overload,
Here we go to __call, when an object calls a method, and the method does not exist, the program automatically invokes the __call
"The official does not recommend use"
PHP has the following several magic constants: __line__ __file__ __dir__ __function__ __class__, etc.
Cases:
Copy Code code as follows:

<?php
class a{
function Test1 ($p) {
echo ' test1<br/> ';
}
Function Test2 ($p) {
echo ' test2<br/> ';
}
Function __call ($method, $p) {
//here $p array, above two variable names can be customized
if ($method = = ' Test ') {
if (count ($p) ==1) {
$this->test1 ($p);
} else if (count ($p) ==2) {
$this->test2 ($p);
}
}
}
}
$a =new a ();
$a->test (5);
$a->test (3,5);
?>

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.