Home  >  Article  >  Backend Development  >  PHP interface language and application scenarios

PHP interface language and application scenarios

不言
不言Original
2018-04-26 11:13:431898browse

This article introduces the interface language and application scenarios of PHP. It has certain reference value. Now I share it with you. Friends in need can refer to it

PHP interface language and application scenarios

// =Specific syntax and characteristics of the interface= //

/*
Specific syntax of the interface:
1. Take human as class Human is a sketch of human being
And the interface is Parts
can be combined with a variety of parts to create a new species.

2. As above, the interface itself is abstract,
The internally declared methods are also abstract by default.
No need to add abstract

3. A class can implement multiple interfaces at one time.
The syntax is implemented with implements
class class name implements interface1,interface2,interface3 {

}
Then implement the functions of the interface.

4. The interface can also be inherited, using extends

5. The interface is a description of a bunch of methods, and attributes cannot be added

6 , The interface is for assembly into classes, and the methods can only be public
*/

interface animal {
    //public $age = 2;
    public function eat();}interface monkey extends animal {
    public function run();
    public function cry();}interface wisdom {
    public function think();}interface bird extends animal {
    public function fly();}class Human implements monkey,wisdom {
    public function eat() {
        echo '吃';
    }    public function run() {
        echo '跑';
    }    public function cry() {
        echo '哭';
    }    public function think() {
        echo '思考';
    }
}


// =Application scenario of the interface: making an abstract database class= //

/*
Building a website
What database should I use?

You can develop with mysql first, and then change the database later.

As long as you use abstract classes to develop, db abstract classes are developed.
*/

abstract class db {
    public abstract function connect($h,$u,$v,$p);

    public abstract function query($sql);

    public abstract function close();}

/*
No matter what database is actually used after going online
Just write the following class according to db

The business logic layer does not need to be changed, because it all implements the db abstract class.
*/

class oracle extends db {}class mssql extends db {}class postsql extends db {}


// The mysql class required to be written strictly corresponds to the db class

class mysql extends db {
    public function connect($h,$h,$h,$h) {
        return true;
    }    public function query($sql) {

    }    public function close() {

    }
}


// = Application scenario of the interface: Production of social networking sites = //

/*
User processing in social networking sites is the core application

Login
Exit
Write a letter
Read the letter
Hello
Change the mood
Eating
Swearing
Making trouble
showing love
flirting

So many methods are the user’s methods,
However, the analysis user cannot use so many at one time Method
So we have to classify them

User information category: (log in, write a letter, read a letter, greet, change mood, exit)
User entertainment category: (log in, curse, make trouble, Show love, tease, exit)
*/

interface UserBase {
    public function login($u,$p);

    public function logout();}interface UserMsg {
    public function writeMsg($to,$title,$content);
    public function readMsg($from,$title);}interface UserFun {
    public function spit($to);
    public function showLove($to);}

As the caller, I don’t need to know your user information class, user entertainment class,
I can know how to call these two classes

Because: these two classes must implement the above interface
Through this interface, development can be standardized.
*/
PHP interface language and application scenarios

Related recommendations:

The difference between abstract classes and interfaces in php

The above is the detailed content of PHP interface language and application scenarios. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn