Learn about the PHP interface

Source: Internet
Author: User
Keywords Learn about the PHP interface

Recently learning PHP5 interface in the process of encountering difficulties, the book is a way to achieve multiple inheritance, but I still do not know how to achieve the specific. In the online check PHP interface data is very few, the Java, in fact, basically almost. After reading the "Clarification Java (interface and inheritance)" This article suddenly dawned on me, originally I understand in error, so-called multiple inheritance refers to the interface inherits the class, but not the class inherits the interface.
The article refers to Oo abstraction, as the article in the sentence-"abstraction is the part of the smoke", very image, the previous thought that the abstract is always difficult to understand, abstract, haha, now it is easy to understand, which is the interface and abstract class to do things.
There are many points in the article that have benefited me, as listed below:
The essence of Oo, I think, is the abstraction of an object.
The function of the interface, Word, is the category of the Flag class (Type of Class). By attributing different types of classes to different interfaces, you can better manage them.
The meaning of inheritance also lies in abstraction, not code reuse.
After reading this article, now basically understand the interface, abstract class, inherit how to apply.

The original text reads as follows:
Clarifying Java (interface and inheritance) the brother of the Institute of Computer Science and I discussed Java, a meeting, a few questions are all about the interface, what is the use of interfaces? Why use an interface? When should I use an interface? I'm glad they're not asking me. How Java connects to SQL Server, or how to develop the EE application, this kind of problem has the lethality, avoids the auspicious. This year the computer college undergraduate has a graduation project is to do J2ME, choose this topic students at the end of May are still suffering face research java.util.* this bag, this ... Alas.

Most people think that the meaning of an interface is to replace multiple inheritance. It is well known that Java does not have multiple inheritance mechanisms like C + +, but it can implement multiple interfaces. In fact, this is very far-fetched, interface and inheritance is a completely different thing, interface does not have the ability to replace multiple inheritance, nor this obligation. The function of the interface, Word, is the category of the Flag class (Type of Class). By attributing different types of classes to different interfaces, you can better manage them. The essence of Oo, I think, is the abstract of the object, the most can reflect this is the interface. Why we discuss design patterns are only for the language with the ability to abstract (such as C + +, Java, C #, etc.), because the design pattern of the study, in fact, is how to reasonably abstract. (Cowboy's famous saying is "Abstraction is the part of the drawing of the image", seemingly ridicule, rationale).

The most basic of the design pattern is the Factory mode (Factory), in my recent very simple application, I want to try to let my program can be migrated across multiple databases, of course, this involves a lot of problems, just how to compatible with different DBMS SQL is a headache. We might as well start by simplifying the problem and only consider how to connect the different databases.

Suppose I have a lot of classes, namely Mysql.java, Sqlserver.java, Oracle.java, Db2.java, they connect separate databases, return a Connection object uniformly, and have a close method to close the connection. Just choose a different class for your DBMS, and you can use it, but what database does my user use? I do not know, I hope that as little as possible to modify the code, will be able to meet his needs. I can abstract the following interfaces:
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, there is no meaningful code, and the specific code is given by the classes that implement this interface, such as Mysql.java:

Package org.bromon.test;
Import java.sql.*;
public class Mysql implements DB
{
PRivate String url= "Jdbc:mysql: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
}

public void Close ()
{
Close the database
}
}

And, of course, Oracle.java, and so on, interface DB has a class for these classes, and in the application we define the object:

Org.bromon.test.DB MyDB;

Using MyDB to manipulate the database, you can use no control over which class I am actually using, which is called the "open-close" principle. But the problem is that the interface is not instantiated, Mydb=new DB (), and the code is absolutely wrong, we can only mydb=new Mysql () or Mydb=new Oracle (). Trouble, I still need to specify which class to instantiate, using the interface as useless. So we need a factory:

Package org.bromon.test;
public class Dbfactory
{
public static DB Connection Getconn ()
{
Return (New Mysql ());
}
}

So the instantiated code becomes: Mydb=dbfactory.getconn ();
This is the most basic common factory in 23 modes (Factory), the factory class is responsible for the specific instantiation of which class, while the other program logic is to operate on the interface of the DB, which is "programming for the interface." Responsibility is passed on to the factory class, of course you can continue to define the factory interface, continue to throw responsibility, which evolved into an abstract factory (abstract Factory).

The entire process of the interface is not responsible for any specific operations, other programs to connect to the database, only need to construct a DB object is OK, regardless of how the factory class changes. This is the meaning----abstraction of the interface.

The concept of inheritance does not have to be much to say, well understood. Why should we inherit it? Because you want to reuse code? This is definitely not a reason, and the meaning of inheritance lies in abstraction, not code reuse. If object A has a run () method, object B also wants to have this method, so someone has a class B extends a. This is done without the brain. If you instantiate a A in B, call A's run () method, can you achieve the same purpose? As follows:
Class B
{
A a=new a ();
A.run ();
}

This is the use of the aggregation of classes to reuse code, is the prototype of the delegation model, is the practice of GOF has always advocated.

What is the meaning of inheritance? In fact, this is caused by historical reasons, the first OO language only inherit, no interface, so can only be inherited to achieve abstraction, please note that the intention of inheritance is abstract, rather than code reuse (although inheritance also has this effect), this is a lot of Java Bad Book one of the most serious mistakes, they caused the shadow, I have not completely gotten rid of, bad books harm Ah, especially into the category, pernicious influence too big. When should I use inheritance? Used only in abstract classes, and in other cases as far as possible. Abstract classes are also not instantiated, it only provides a template, which is very good to explain the problem.

The root of all the evils of software development, one is to repeat the code instead of reusing the code, and the other is rotten with inheritance, especially for C + + programmers. In Java, it is very sensible to outlaw multiple inheritance in order to stop rotten inheritance, but many people do not understand it. Java can be a better embodiment of design, which is one of the reasons I am fascinated.

  • 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.