PHP object-oriented Advanced Learning (extract class, interface, final, class constants) _php techniques

Source: Internet
Author: User
Tags inheritance modifier iusb
First, draw the image class (abstract)
In our actual development process, some classes do not need to be instantiated, such as the previous learning of some of the parent classes, mainly for subclasses to inherit, which can improve code reusability
Syntax structure:
Copy Code code as follows:

Abstract class class Name {
attribute $name;
Method () {}//method can also be an abstract modifier function method name () {}
}

Cases:
Copy Code code as follows:

Abstract class animal{
Public $name;
Public $age;
Abstract methods can not have a method body, mainly to let subclasses to achieve;
Abstract public Function cry ();
Abstract classes can contain abstract methods, and can also contain instance class methods
Public Function GetName () {
Echo $this->name;
}
}
Class cat{
Public Function Cry () {
echo ' OK ';
}
}

Understanding: An animal, in fact, is an abstract concept that prescribes the common attributes and behaviors of some animals, but in fact it itself has confiscated those attributes and behaviors. Such as: vehicles, plants and so on

Attention:
1. If a class is decorated with abstract, then the class is an abstract class, and if a method is modified by abstract, then the method is an abstraction, and the abstract method cannot have the method body => the abstract Function cry (); even {} can not have
2. Abstract classes must not be instantiated, abstract classes can have no abstract method, but if a class contains any abstract method, the class must be declared as an abstract class;
3. If a class inherits another abstract class, the subclass must implement all of the abstract methods in the abstract class (unless it itself is also declared as an abstract class);

second, interface (interface)
The interface is to encapsulate some methods that are not implemented, and then write them out according to the situation when the class is to be used.
Grammatical structure
Interface interface Name {
Properties, Methods
Methods in an interface cannot have a method body;
}
How to implement an interface
Class name implements interface name {

}
Understanding: An interface is a more abstract abstract class, and a method in an abstract class can have a method body, but a method in an interface must have no method body. The interface realizes the design idea of polymorphism, high cohesion and low coupling of programming.

Cases:
Copy Code code as follows:

An interface is defined as a specification, a property, and generally begins with a lowercase i;
Interface iusb{
Public function start ();
Public function Stop ();
}
Write the camera class and let it implement the interface
When a class implements an interface, the class must implement all the methods of the interface
Class Camera implements iusb{
Public Function Start () {
Echo ' Camera Start Work ';
}
Public Function Stop () {
Echo ' Camera Stop Work ';
}
}
Write a mobile phone class
Class Phone implements iusb{
Public Function Start () {
Echo ' Phone satrt Work ';
}
Public Function Stop () {
Echo ' Phone Stop Work ';
}
}
$c =new Camera ();
$c->start ();
$p =new Phone ();
$p->start ();

When to use an interface:
1, set the specifications, so that other programmers to achieve
2, when multiple peer class, all need to implement a function, but the way to achieve different;

Summary:
1, the interface can not be instantiated, all the methods in the interface can not have the main;
2. A class can implement multiple interfaces, separating class demo implements if1,if2,if3{} with commas (,)
3, the interface can have properties, but must be constants, constants can not have modifiers (the default is the public modifier)
such as: interface iusb{
Const A=90;
}
Echo iusb::a;
4, the method in the interface must be public, the default is public;
5, an interface cannot inherit other classes, but can inherit other interfaces, an interface can inherit multiple other interfaces
Such as: interface interface name extends if1,if2{}
6. A class can implement other interfaces while inheriting the parent class
such as: Class Test extends Testbase implements test1,test2{}

Implementing Interfaces vs Inheritance Classes
The inheritance of PHP is a single inheritance, that is, a class can only inherit a parent class, which has a certain effect on the extension of the subclass function. Implementing an interface can be viewed as a supplement to an inherited class. Inheritance is a hierarchical relationship, not too flexible, and the implementation of interface is a peer relationship, implementation of the interface can not break the premise of the inheritance relationship, to a function expansion, very flexible.

Third, Final

1. If we want a class not to be inherited by other classes (for security reasons, for example). ), you may consider using final
Grammar:
Final Class a{}
2, if we want a method, do not quilt class rewrite, you can consider using final decoration, the final modified method can still be inherited, because the method of inheritance depends on the modification of the public
Such as:
Copy Code code as follows:

Class a{
Final public Function getrate ($salary) {
return $salary *0.08;
}
}
Class B Extens a{
The Getrate method of the parent class here uses final, so there's no way to rewrite getrate
Public Function Getrate ($salary) {
return $salary *0.01;
//}
}

3, final can not be used to modify attributes

four, class constants (const)

In some cases, there might be a need to: when you do not want a member variable to be modified, and you want the value of the variable to be fixed, you can use the const constant (the constant name should be all uppercase and without the $ symbol, and the constant cannot be decorated)
Grammar:
const constant name = constant value; The initial value must be assigned because the constants cannot be modified
Call:
Class Name:: Constant name [available self:: constant name] or interface name:: Constant name//interface can use constants only, cannot use variables

Such as:
Copy Code code as follows:

Class a{
Const tax_rate=0.08;
function Paytax ($salary) {
return $salary *self::tax_rate;
}
}
$a =new A ();
echo $a->paytax (100);

Note:
1, constants can be inherited by the quilt class
2. A constant belongs to a class, not to an object
Related Article

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.