On the concept of abstract class and interface in PHP and the difference _php skills

Source: Internet
Author: User
Tags inheritance
Copy Code code as follows:

Definition of abstract class:
Abstract class ku{//Defining an abstraction
Abstract function kx ();
......
}
function AA extends ku{
Ways to implement abstract classes
function kx () {
Echo ' SDSF ';
}
}
How to use
$aa =new AA;
$aa->kx ();
1. Define methods, subclasses must fully implement all the methods in this abstraction
2. You cannot create an object from an abstract class, it is meant to be extended
3. Abstract classes usually have an abstract method without curly braces
4. Abstract methods do not need to implement specific functions, subclass to complete
5. When a subclass implements a method of an abstract class, the visibility of its subclasses must be greater than or equal to the definition of the abstract method
6. Abstract class methods can have parameters, or can be empty
7. If the abstract method has parameters, the implementation of the subclass must also have the same number of parameters
Definition of interface class:
Interface shop{
Public function ($gid);
Public function sell ($GID);
Abstract function view ($gid);
}
If you want to use an interface, you must define a few of the methods in the interface class that are not available (except abstract).
So if in a big project regardless of how others do the following method, but he must implement all the methods in this interface can!
Example: one way to implement the above interface
Class Baseshop implements shop{
Public function Buy ($gid) {
Echo ' you purchased the ID for: '. $gid. ' Commodity ';
}
Public function Sell ($gid) {
Echo ' you buy for sale ID: '. $gid. ' Commodity ';
}
Public Function View ($gid) {
Echo ' You browsed the ID as: '. $gid. ' Commodity ';
}
}
Examples of multiple inheritance of interfaces:
<?php
Interface staff_i1{//interface 1
function SetID ();
function GetID ();
}
Interface staff_i2{//interface 2
function SetName ();
function GetName ();
}
Class staff implements staff_i1,staff_i2{
Private $id;
Private $name;
function SetID ($id) {
$this->id = $id;
}
function GetID () {
return $this->id;
}
function SetName ($name) {
$this->name = $name;
}
function GetName () {
return $this->name;
}
function Otherfunc () {//This is a method that does not exist in an interface
echo "Test";
}
}
?>

their different points:
1. Abstract classes can have non abstract methods, and interfaces can only have abstract methods!
2. A class can inherit multiple interfaces, and a class can inherit only one abstract class!
3. The use of the interface through the Implements keyword, the abstract class is through the inheritance extends keyword!

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.