Php object-oriented image class, interface, final, class constant _ PHP Tutorial

Source: Internet
Author: User
Tags iusb
Php object-oriented image class, interface, final, class constant. This article introduces the basic tutorials on image classes, interfaces, final, and class constants in php. For more information, see. Php object-oriented 1. image extraction class (abstract) in our real article, we will introduce the basic tutorials on the image extraction class, interface, final, and class constants in php. if you need to know more, please refer to it. Php object-oriented

1. image extraction class (abstract)
In our actual development process, some classes do not need to be instantiated. for example, some of the parent classes we learned earlier mainly involve subclass inheritance, which improves code reusability.
Syntax structure:

The code is as follows:
Abstract class name {
Attribute $ name;
Method () {}// method can also be the abstract modifier function method name (){}
}

Example:

The code is as follows:
Abstract class animal {
Public $ name;
Public $ age;
// The abstract method cannot have a method body, mainly to implement the subclass;
Abstract public function cry ();
// The abstract class can contain abstract methods and instance class methods.
Public function getname (){
Echo $ this-> name;
}
}
Class Cat {
Public function cry (){
Echo 'OK ';
}
}


Understanding: animals are actually an abstract concept that specifies the common attributes and behaviors of some animals, but in fact they themselves do not possess those attributes and behaviors. For example, transportation tools and plants

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

2. interfaces)
An interface encapsulates unimplemented methods into a class and writes them according to the actual situation;
Syntax structure

The code is as follows:
Interface name {
// Attributes and methods
// Methods in the interface cannot have a method body;
}
How to implement interfaces
Class name implements interface name {

}

Understanding: An interface is a more abstract class. methods in an abstract class can have a method body, but methods in the interface must have no method body. The interface implements the design idea of Program Design with polymorphism, high cohesion, and low coupling;

Example:

The code is as follows:
// The interface defines specifications and attributes, generally starting with lower-case I;
Interface iUsb {
Public function start ();
Public function stop ();
}
// Compile the camera class to implement the interface
// When a class implements an interface, 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 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 standards to be implemented by other programmers
2. when multiple classes at the same level need to implement a function, but the implementation methods are different;

Summary:
1. the interface cannot be instantiated, and all methods in the interface cannot have a subject;
2. a class can implement multiple interfaces, separated by commas (,). class demo implements if1, if2, if3 {}
3. the interface can have attributes, but must be constants. constants cannot have modifiers (the default is the public modifier)
For example: interface iUsb {
Const A = 90;
}
Echo iUsb:;
4. all methods in the interface must be public. The default value is public;
5. one interface cannot inherit other classes, but it can inherit other interfaces. one interface can inherit multiple other interfaces.
For example: interface name extends if1, if2 {}
6. a class can implement other interfaces while inheriting the parent class
Example: class test extends testbase implements test1, test2 {}

Implementation interface VS inheritance class
The inheritance of php is a single inheritance, that is, a class can only inherit one parent class, which affects the extension of the subclass function. The implementation interface can be seen as a supplement to the inherited class. Inheritance is a hierarchical relationship, which is not flexible, while the implemented interface is a hierarchical relationship. the implemented interface can be extended to a function without breaking the inheritance relationship.

III. Final

1. if we want a class not to be inherited by other classes (for security reasons, etc ..), You can consider using final.
Syntax:
Final class {}
2. if we want a method not to be overwritten by the quilt class, we can consider using final for modification. The final modification method can still be inherited, because the inheritance right of the method depends on the modification of public.
For example:

The code is as follows:
Class {
Final public function getrate ($ salary ){
Return $ salary * 0.08;
}
}
Class B extens {
// The getrate method of the parent class uses final, so the getrate cannot be rewritten here.
// Public function getrate ($ salary ){
// Return $ salary x 0.01;
//}
}

3. final cannot be used to modify attributes.

IV. const)

In some cases, you may want to change the value of a member variable without changing it, in this case, you can use a const constant (the constant name should be in uppercase without the $ symbol, and the constant cannot be decorated)
Syntax:
Const constant name = constant value; // The initial value must be assigned, because the constant cannot be modified.
Call:
Class name: constant name [internal use of this class self: constant name] or interface name: constant name // The interface can only use constants, not variables

For example:

The code is as follows:
Class {
Const TAX_RATE = 0.08;
Function paytax ($ salary ){
Return $ salary * self: TAX_RATE;
}
}
$ A = new ();
Echo $ a-& gt; paytax (100 );


Note:
1. constants can be inherited by quilt classes.
2. constants belong to a class instead of an object.
Although the implementation is simple, you can easily and quickly implement php object-oriented editing operations with a little basic knowledge.

Bytes. Php object-oriented 1. image extraction class (abstract) in our implementation...

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.