PHP5 object-oriented interface technology

Source: Internet
Author: User
Tags class definition

The PHP5 interface syntax is as follows:
When declaring a class, the keyword we use is "class", while the interface is a special class and the keyword used is "interface ";

Class Definition: class name {... }, Interface declaration: interface name {...}

The code for the specific PHP5 abstract class is as follows:

The code is as follows: Copy code

Abstract class AbstractClass {

Abstract public function test ();

}     

    

Class ImplementedClass extends AbstractClass {

Public function test (){

Echo "ImplementedClass: test () called .";

   }     

}     

    

$ O = new ImplementedClass;

$ O-> test ();


Instance 2

The code is as follows: Copy code

Interface displayable {

Function display ();

}     

Interface printable {

Function doprint ();

}     

    

Class foo implements displayable, printable {

Function display (){

// Code

} Function doprint (){

// Code

  }     

}    

 


The introduction of abstract classes and PHP5 interfaces makes PHP a fully object-oriented language.
// Define an interface using the interface keyword. "One" is the interface name.

The code is as follows: Copy code

Interface One
{
// Define a constant
Const constant = 'constant value ';

// Defines an abstract method "fun1"
Public function fun1 ();

// Defines the abstract method "fun2"
Public function fun2 ();
}

In the above example, an interface "one" is defined, which declares two abstract methods "fun1" and "fun2", because all methods in the interface are abstract methods, therefore, when declaring an abstract method, you do not need to use the "abstract" keyword as the abstract class. This keyword is added by default, in addition, the "public" access permission in the interface can also be removed, because it is public by default, because all the members in the interface are public, for the members in the interface, we cannot use the "private" and "protected" permissions, both of which must be public or default. In addition, we also declare a constant "constant" in the interface. Because variable members cannot be used in the interface, we need to use the const keyword for declaration.

Because an interface is a special abstract class, all the methods in it are abstract methods, so the interface cannot generate instance objects; it is also a standard, and all abstract methods need to be implemented by subclasses.

We can use the "extends" keyword to let an interface inherit from another interface;

The code is as follows: Copy code

// Use extends to inherit another interface
Interface Two extends One
{
Function fun3 ();
Function fun4 ();
}

We define a subclass of an interface to implement the keyword "implements" used by all abstract methods in the interface, rather than the "extends" mentioned above ";

// Use the keyword "implements" to implement the abstract method in the interface

The code is as follows: Copy code

Class Three implements One
{
Function fun1 ()
    {
... ...
    }

Function fun2 ()
    {
... ...
    }
}

// All methods are implemented. We can use subclasses to instantiate objects.

The code is as follows: Copy code
$ Three = new Three ();

We can also use abstract classes to implement partial abstract methods in interfaces. However, to instantiate an object, this abstract class must have a subclass to implement all its abstract methods;

As we have mentioned earlier, PHP is a single inheritance. A class can only have one parent class, but a class can implement multiple interfaces, which is equivalent to a class complying with multiple specifications, just as we must not only abide by national laws, but also comply with school rules if we are in school;

The code is as follows: Copy code

// Use implements to implement multiple interfaces
Class Four implemtns Interface 1, Interface 2 ,......
{
// The object can be instantiated only when all methods in the interface are implemented.
}

In PHP, not only can a class implement multiple interfaces, but also can inherit a class and implement multiple interfaces at the same time. You must inherit the class before implementing the interface;

The code is as follows: Copy code

// Use extends to inherit a class and implements to implement multiple interfaces
Class Four extends class name 1 implemtns Interface 1, Interface 2 ,......
{
// Objects can be instantiated only when methods in all interfaces are implemented
.........
}

After talking so much, let's finally look at an instance.


VideoCard. php interface file (function interface definition of the video card)

The code is as follows: Copy code
<? Php
Interface VideoCardInter {
Function Display ();
Function getName ();
}
?>

Dmeng. php implementation interface (how can the Emon manufacturer implement these interfaces? The motherboard manufacturer does not need to care about them)

The code is as follows: Copy code
<? Php
Include_once ("VideoCard. php ");
Class Dmeng implements VideoCardInter {
Function Display (){
Echo "Display ";
}
Function getName (){
Return "Dmeng VideoCard ";
}
}
?>

Mainboard. php application interface (plug the video card into the motherboard, the motherboard only needs to use these interfaces, you can also do not need)

The code is as follows: Copy code
<? Php
Include_once ("VideoCard. php ");
Include_once ("Dmeng. php ");
Class Mainboard {
Var $ vc;
Function run (VideoCardInter $ vc) {// defines the VideoCardInter interface type parameter. At this time, you do not know who will implement it.
$ This-> vc = $ vc;
$ This-> vc-> Display ();
Echo "main board running! ";
}
}
$ Conputer = new Mainboard ();
$ Conputer-> run (new Dmeng );

// When used, write the name of the implemented interface class. (the video card of EMR can also be changed to another home, as long as they all implement the interface)
?> The above is self-converted, and then I tried to write something: summarize my thoughts
Here I want to divide it into four parts:

1. Static link database class (basic link database, number of query records, returned result set, etc)

2. Interfaces of each module (for example, NewsInterface)

3. Class that implements the interface (NewsInterfaceImpl, the news implementation class, writes some methods used in the page, and then the new object calls the method here)

4. Module (News)

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.