This article introduces the content of the PHP interface language and application scenarios, has a certain reference value, now share to everyone, the need for friends can refer to
= specific syntax and characteristics of the interface =//
/*
Specific syntax for the interface:
1, human as a class Human is the sketch of man
And the interface is part
A new species can be combined with a variety of parts.
2, as above, the interface itself is abstract,
The method of internal declaration is also abstract by default.
No need to add abstract
3, a class can implement multiple interfaces at once.
Syntax is implemented with implements
Class Name implements Interface1,interface2,interface3 {
}
Then the function of the interface is implemented.
4, the interface can also inherit, with extends
5, interface is a description of a bunch of methods, can not add attributes
6, the interface is for assembly into class use, the method 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 ' eats '; } Public Function Run () { echo ' run '; } Public Function Cry () { echo ' cry '; } Public function Think () { echo ' think '; }}
= Application scenario of the interface: do an abstract database class =//
/*
Do website
What database do you use?
Can be developed first with MySQL, and then change the database later.
As long as the abstract class development, DB abstract class development.
*/
Abstract class DB {public abstract function connect ($h, $u, $v, $p); public abstract function query ($sql); Public abstract function Close ();}
/*
No matter what database you actually use after you go online
Just write a copy of the following class in DB
The business logic layer does not have to be changed, because it is the implementation of the DB abstract class.
*/
Class Oracle extends DB {}class MSSQL extends DB {}class Postsql extends db {}
The MySQL class required to write exactly corresponds to the DB class Strictly
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: The Making of social networking sites =//
/*
The processing of users in social networking sites is the core application
Landing
Exit
Write
Read the letter
Say hello
Change mood
Eat
Curse
Trouble
Show Love
Sexy
So many methods are the user's method,
However, analytics users can't use so many methods at once
So we're going to sort them out.
User Information class: (Login, write, read, greet, change mood, exit)
User Entertainment Category: (Login, Curse, 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 a caller, I don't need to know your user information class, User entertainment class,
I can know how to call these two classes
Because: all of these two classes implement the above interface
Through this interface, it is possible to standardize development.
*/