PHP Advanced Object-oriented learning (pumping like class, interface, final, class constants) _php tutorial

Source: Internet
Author: User
Tags iusb
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:
Copy CodeThe code is as follows:
Abstract class class Name {
attribute $name;
Method () {}//method can also be the abstract modifier function method name () {}
}

Cases:
Copy CodeThe code is as follows:
Abstract class animal{
Public $name;
Public $age;
Abstract methods can not have 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 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.

Attention:
1, if a class is modified 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 the method body and 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 a number of methods that are not implemented, packaged together, to a class to use, and then according to the specific circumstances of these methods written out;
Grammatical structure
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:
Copy CodeThe code is as follows:
An interface is a definition of a specification, a property, usually preceded by 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 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 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:
Copy CodeThe 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 there is no way to rewrite the 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:
Copy CodeThe 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 quilt class
2. Constants belong to a class, not to an object

http://www.bkjia.com/PHPjc/325503.html www.bkjia.com true http://www.bkjia.com/PHPjc/325503.html techarticle first, the extraction 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 class, mainly to let the subclass to inherit, this can improve ...

  • 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.