Home  >  Article  >  Backend Development  >  Advanced learning of PHP object-oriented (abstract classes, interfaces, final, class constants)_PHP tutorial

Advanced learning of PHP object-oriented (abstract classes, interfaces, final, class constants)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:18:19897browse

1. Abstract class (abstract)
In our actual development process, some classes do not need to be instantiated. For example, some of the parent classes learned earlier are mainly inherited by subclasses. This can improve code reusability
Syntax structure:

Copy code The code is as follows:

abstract class class name {
Attribute $name;
Method(){} //Method can also be abstract modifier function method name(){}
}

Example:
Copy code The code is as follows:

abstract class animal{
public $name;
public $age;
/ /Abstract methods cannot have method bodies, mainly for subclasses to implement;
abstract public function cry();
//Abstract classes can contain abstract methods and instance class methods
public function getname(){
echo $this->name;
}
}
class Cat{
public function cry(){
echo 'ok';
}
}

Understanding: Animal class is actually an abstract concept. It stipulates some common attributes and behaviors of some animals, but in fact it does not have those in itself. properties and behaviors. Another example: vehicles, plants, etc.

Note:
1. If a class is modified with abstract, then the class is an abstract class. If a method is modified with abstract, then the class A method is an abstract method. An abstract method cannot have a method body => abstract function cry(); not even {}
2. An abstract class must not be instantiated. An abstract class can have no abstract method, but if A class contains any abstract method, and this class must be declared as an abstract class;
3. If a class inherits another abstract class, the subclass must implement all abstract methods in the abstract class (unless it itself Also declared as an abstract class);

2. Interface (interface)
Interface is to encapsulate some unimplemented methods together. When a certain class is used, Then write these methods according to the specific situation;
Grammar structure
interface interface name {
//Attributes, methods
//Methods in the interface cannot have method bodies;
}
How to implement the interface
class class name implements interface name {

}
Understanding: Interface is a more abstract abstract class. Methods in abstract classes can have method bodies, but methods in interfaces There must be no method body. The interface implements the polymorphism of programming and the design ideas of high cohesion and low coupling;

Example:
Copy code The code is as follows:

//Interface defines specifications and attributes, usually starting with a lowercase i;
interface iUsb{
public function start();
public function stop();
}
//Write a camera class and let it implement the interface
//When a class implements an interface, then the class must implement all 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 interfaces:
1. Set specifications and let other programmers implement them
2. When multiple parallel classes need to implement a certain function, but the implementation methods are different ;

Summary:
1. Interfaces cannot be instantiated, and all methods in the interface cannot have bodies;
2. A class can implement multiple interfaces, separated by commas (,) demo implements if1,if2,if3{}
3. The interface can have attributes, but they must be constants. Constants cannot have modifiers (the default is public modifier) ​​
For example: interface iUsb{
const A=90;
}
echo iUsb::A;
4. All methods in the interface must be public, and the default is public;
5. An interface cannot inherit other classes, but You can inherit other interfaces, and one interface can inherit multiple other interfaces
For example: interface The interface name extends if1,if2{}
6. A class can inherit other interfaces while inheriting the parent class
For example: class test extends testbase implements test1,test2{}

Implementing interface VS inheriting class
PHP inheritance is single inheritance, that is, a class can only inherit one parent class, which can extend the functions of subclasses. certain influence. Implementing interfaces can be seen as a supplement to inherited classes. Inheritance is a hierarchical relationship and is not very flexible, while implementing interfaces is a horizontal relationship. Implementing interfaces can extend a certain function without breaking the inheritance relationship, which is very flexible.

3. Final

1. If we want a class not to be inherited by other classes (for example, for security reasons, etc.), then we can consider using final
Syntax:
final class A{}
2. If we want a method not to be overridden by subclasses, we can consider using final to modify it. The method modified by final can still be inherited, because the method Inheritance rights depend on public modification
such as:
Copy code The code is 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 here Can no longer override getrate
//public function getrate($salary){
// return $salary*0.01;
//}
}

3. final cannot be used to modify attributes

4. Class constants (const)

In some cases, there may be such a requirement: when you do not want a member variable to be Modify, if you hope that the value of the variable is fixed, you can use const constants (constant names should be in all uppercase letters and without the $ symbol, and constants cannot be modified)
Syntax:
const constant name = constant value; // An initial value must be assigned because constants cannot be modified
Call:
Class name::constant name [self::constant name is available inside this class] or interface name::constant name //Only constants can be used in the interface, variables

cannot be used, such as:
Copy code The code is 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 subclasses
2. Constants belong to a certain class of, rather than belonging to an object

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325503.htmlTechArticle1. Abstract classes (abstract) In our actual development process, some classes do not need to be instantiated. For example, some parent classes learned earlier mainly let subclasses inherit, which can improve...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn