Home > Backend Development > PHP Tutorial > Learning the PHP interface_PHP tutorial

Learning the PHP interface_PHP tutorial

WBOY
Release: 2016-07-13 17:33:56
Original
1038 people have browsed it

Recently I encountered difficulties while learning the PHP(as the current mainstream development language)5 interface. The book said it is a way to achieve multiple inheritance, but I still don’t know how to do it. accomplish. I looked up php (as the current mainstream development language) interface information on the Internet. There was very little information on it, so I checked java. In fact, they are basically the same. After reading the article "Clarifying Java (Interfaces and Inheritance)", I suddenly realized that I had misunderstood it from the beginning. The so-called multiple inheritance refers to interfaces inheriting classes, not classes inheriting interfaces.
The abstraction of OO was mentioned in the article. Just like the sentence in the article - "Abstraction is to remove the image part". It is very vivid. When I thought about abstraction, I always thought it was difficult to understand. It is abstract, haha, now it is It's easy to understand, and this is exactly what interfaces and abstract classes do.
There are many other points of view in the article that have benefited me a lot, as listed below:
The essence of OO, I think, is the abstraction of objects.
The role of the interface, in a nutshell, is to mark the type of class. Attributing different types of classes to different interfaces can better manage them.
The meaning of inheritance is abstraction, not code reuse.
After reading this article, I now basically understand how to apply interfaces, abstract classes, and inheritance.

The original text is as follows:
Clarifying Java (Interfaces and Inheritance) My brother, a second-year graduate student in the School of Computer Science, discussed Java with me. When we met, several questions were all about interfaces. What is the use of interfaces? Why use interfaces? When should you use interfaces? I'm glad that they didn't ask me how to connect to SQL server(a powerful database platform on the WINDOWS platform) using Java, or how to develop J2EE applications. Such questions are lethal and should be avoided. This year, the School of Computer Science has a graduation project project on J2ME. The students who chose this topic were still studying the java.util.* package with a grimace at the end of May, this and this... sigh.

Most people think that the purpose of interfaces is to replace multiple inheritance. As we all know, Java does not have a multiple inheritance mechanism like C++, but it can implement multiple interfaces. In fact, this is far-fetched. Interfaces and inheritance are completely different things. Interfaces have no ability to replace multiple inheritance, and they have no such obligation. The function of the interface, in a nutshell, is to mark the type of class. Attributing different types of classes to different interfaces can better manage them. I think the essence of OO is the abstraction of objects, and the interface best embodies this. Why we discuss design patterns only for languages ​​with abstract capabilities (such as c++, java, c#, etc.) is because what design patterns study is actually how to abstract reasonably. (Cowboy's famous saying is "Abstraction is to remove the image part", which seems to be a joke, but is actually true).

The most basic design pattern is the Factory pattern. In my recent very simple application, I wanted to try my best to make my program portable between multiple databases. Of course, this involves many problems. Just how to be compatible with SQL of different DBMS is a headache. We might as well simplify the problem first and only consider how to connect different databases.

Suppose I have many classes, namely MySQL (the best combination with PHP) .java, SQLServer.java, Oracle (large website database platform) .java , DB2.java, they connect to different databases respectively, uniformly return a Connection object, and all have a close method for closing the connection. You only need to select different classes for your DBMS, and you can use it. But what database will my user use? I don't know, what I hope is to modify the code as little as possible to meet his needs. I can abstract the following interface:
package org.bromon.test;
public interface DB
{
 java.sql.Connection openDB(String url,String user,String password);
void close();
}

This interface only defines two methods without any meaningful code. The specific code is given by the class that implements this interface, such as MySQL (the best combination with PHP) .java:

Package org.bromon.test;
import java.sql.*;
public class MySQL(The best combination with PHP) implements DB
{
private String url=”jdbc:MySQL(The best combination with PHP):localhost:3306/test”;
 private String user=”root”;
private String password=” ”;
 private Connection conn;
 public Connection openDB(url,user,password)
 {
  //Code to connect to the database
 }

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508534.htmlTechArticleRecently, I encountered difficulties in the process of learning PHP (as the current mainstream development language) 5 interface. The book says It is a way to implement multiple inheritance, but I still don't know how to implement it. ...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template