This article introduces the basic tutorial of drawing class, interface, final, and class constants in PHP, and you can refer to the friends who need to know. PHP Object-oriented
First, the extraction class (abstract)
In our actual development process, some classes do not need to be instantiated, such as some of the previous learning of the parent class, mainly for the subclass to inherit, so as to improve code reusability
Syntax structure:
| The code is as follows |
Copy Code |
Abstract class class Name { attribute $name; Method () {}//method can also be the abstract modifier function method name () {} }
|
Cases:
| |
copy code |
abstract class animal{ public $name; Public $age; //Abstract methods can not have method body, mainly to let subclasses to achieve; Abstract Public Function cry (); The //abstract class can contain abstract methods and can also contain instance class methods Public Function GetName () { echo $this->name; } } Class cat{ Public Function Cry () { echo ' OK '; } } |
understands that animal is actually an abstract concept that prescribes some of the common properties and behaviors of some animals, but in fact it itself and the confiscation of those attributes and behaviors. Another example: transportation, plants, etc.
Note:
1, if a class is decorated with abstract, then the class is an abstract class, if a method is modified by the abstract, then the method is an abstraction method, the abstract method cannot have method BODY = abstract function Cry (), even {} can not have
2, abstract class must not be instantiated, abstraction class can have no abstract method, but if a class contains any abstract method, this class must be declared as an abstract class;
3. If a class inherits another abstract class, the subclass must implement all the abstract methods in the abstract class (unless it declares itself as an abstract class);
Two, Interface (interface)
interfaces are methods that are not implemented. Encapsulated together, to a class to use, and then according to the specific circumstances of these methods are written;
Syntax Structure
| The code is as follows |
Copy Code |
Interface interface Name { Properties, Methods The method in the interface cannot have the method body; } How to implement an interface Class name implements interface name {
}
|
Understanding: Interfaces are abstract classes that are more abstract, and methods in an abstract class can have a method body, but the methods in an interface must have no method body. The interface realizes the design idea of multi-state and high cohesion and low-coupling of programming.
Cases:
| code as follows |
copy code |
//interfaces are defined specifications, attributes, usually beginning with lowercase i; interface iusb{ Public Function Start (), Public Function Stop (); } //Write camera class, let it Implement Interface //When a class implements an interface, the class must implement all methods of the interface class Camera implements iusb{ Public Function start () { br> Echo ' Camera Start work '; } Public Function Stop () { echo ' Camera stop work '; } } //write a 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 the interface:
1, set specifications, let other programmers to implement
2, when multiple peer class, all need to implement a function, but the implementation of the same way;
Summary:
1, interface cannot be instantiated, All methods in an interface cannot have a body,
2, a class can implement multiple interfaces, separated by a comma (,) class demo implements if1,if2,if3{}
3, the interface can have attributes, but must be constants, Constants can not have modifiers (default is the public modifier)
such as: interface iusb{
const a=90;
}
Echo iusb::a;
4, the methods 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{}
Implement Interface vs Inherit class
The inheritance of PHP is a single inheritance, that is, a class can only inherit a parent class, so that the extension of the function of the subclass has a certain effect. Implementing an interface can be seen as a complement to an inherited class. Inheritance is a hierarchical relationship, not too flexible, and implementation of the interface is a level of relations, implementation of the interface can not break the inheritance relationship under the premise of a function extension, very flexible.
Third, Final
1, if we want a class to not be inherited by another class (for security reasons, for example). ), you might consider using the final
Syntax:
Final class a{}
2, if we want a method to be overridden without a quilt, consider using final to decorate it, or the final decorated method can be inherited. Because the inheritance of the method depends on the adornment of public
such as:
| |
copy code |
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 is no way to rewrite getrate //public function getrate ($salary) { / /return $salary *0.01; //} } |
3. Final cannot be used to modify attributes
Four, class constants (const)
In some cases, there may be a need for a const constant (the constant name should all be capitalized, without the $ symbol, and a constant modifier) when you do not want a member variable to be modified and you want the value of the variable to be fixed.
Grammar:
const constant name = constant value; The initial value must be assigned, because constants cannot be modified
Call:
Class Name:: Constant name [self:: constant name] or interface name:: Constant name//interface only constants can be used and variables cannot be used
Such as:
| The code is as follows |
Copy Code |
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 quilt class
2. Constants belong to a class, not to an object
Although the implementation is very simple, as long as you have a little basic knowledge to facilitate the rapid implementation of PHP object-oriented editing operations.
http://www.bkjia.com/PHPjc/629158.html www.bkjia.com true http://www.bkjia.com/PHPjc/629158.html techarticle This article introduces the basic tutorial of drawing class, interface, final, and class constants in PHP, and you can refer to the friends who need to know. PHP Object-oriented one, the extraction class (abstract) in our real ...