PHP extraction class, interface, final, class constant advanced Learning Tutorial

Source: Internet
Author: User
Tags iusb
PHP Object-oriented advanced learning, mainly including the extraction class, interface, final, class constants of some data

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:

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

Cases:

Abstract class animal{public $name; the public $age;//Abstraction methods cannot have method bodies, mainly for subclasses to implement; abstract public function cry ();//abstract methods can be included in a class , and can also contain instance class methods public function GetName () {echo $this->name;}} Class cat{Public Function Cry () {echo ' OK ';}}

Understanding: An animal is, in fact, 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 abstracted, then the method is an abstract method, the abstract method cannot have method BODY = abstract function Cry () , even {} can not have
2, abstract class must not be instantiated, abstract class can have no abstract method, but if a class contains any abstract method, this class must be declared as abstract class;
3, if a class inherits another abstract class, the subclass must implement all abstract methods in the abstract class (unless it declares itself as an abstract class);

Second, Interface (interface)
Interface is to put some methods that are not implemented, packaged together, to a class to use, and then according to the specific circumstances to write these methods;
Syntax structure
Interface interface Name {
//attribute, method
//interface method cannot have method body;
}
How to Implement Interface
Class name implements interface name {

}
Understanding: Interfaces are abstract classes that are more abstract, and methods in an abstract class can have method bodies, but methods in an interface must have no method body. The interface realizes the design idea of the multi-state and high cohesion and low coupling of the programming.

Example:

//interface is defined specification, attribute, usually start with lowercase i; Interface iusb{public function start (); public function Stop ();  }//write the camera class, let it implement 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 ' P Hone Stop work '; }} $c =new Camera (); $c->start (); $p =new Phone (); $p->start (); 

When to use the interface:
1, set specifications, let other programmers to achieve
2, when a number of peer class, all need to implement a function, but the way of implementation is different;

Summary:
1, the interface can not be instantiated, the interface of all methods can not have the subject;
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 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
Example: 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 Inheriting classes
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 other classes (for security reasons, for example). ), you might consider using the final
Grammar:
Final Class a{}
2, if we want a method, not quilt rewrite, you can consider using final to decorate, the final modified method can be inherited, because the inheritance of the method depends on the decoration of the public
Such as:

Class a{final public Function getrate ($salary) {return $salary *0.08;}} class B Extens a{//The Getrate method of the parent class uses final, so there is no Law 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:

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

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.