Discussion of interfaces in PHP

Source: Internet
Author: User
Discussion of interfaces in PHP
Article reprinted from Chongqing PHP, original address: http://www.php-chongqing.com/index.php/article/107


Many people ask me, what is the use of PHP interface? When do you use it? Where do you use it?
First we look at what is an interface?
Interfaces are not classes, similar to classes. It is mainly used to describe what functions (methods) The class has, but does not implement specific functions, but only defines the function, which is implemented by implementing the class implementation of the interface (implement), and a class can implement one or more interfaces.
There is no interface for the general dynamic language, but in PHP5, the interface is defined in PHP:
Interface User {public    function eat ();    Public function Sleep ();}

Implementing the interface:
Class Chinese implements User {public    function eat () {echo "Eat Chinese food! ";    }    Public Function sleep () {echo "Sleep ..."}    }


What does the above code mean in PHP programming? From a practical point of view, the role of PHP interface is almost 0, is completely chicken, the above code we do not define the interface, directly define a Chinese class can also work normally, without any problems at all. Currently, the interface in PHP5 can only be used as a type hint, only for this purpose. Of course, type hints can make some of our designs clearer.

Let's take a look at how to use the interface to do type hinting (it doesn't really mean much).

Public Function dosomething ($user) {    $user->eat ();}


If the method dosomething () needs to pass an instance of the user object, it is not clear to the user that the $user is an instance of the user object, so it is possible to write the code as follows:

$user = "Bing.peng"; $test->dosomething ($user);

Then when the program tries to execute the $user->eat () method, it fails because the user does not pass in the expected object. Let's add a type hint for the DoSomething () method:
Public Function dosomething (User $user) {    $user->eat ();}

This makes it clear to the user that dosomething () needs a user object to avoid some low-level errors. But because PHP is a dynamic language, you can still write, as follows:
$user = "Bing.peng"; $test->dosomething ($user);

Code execution, you will still fail, the role of the type hint does not make much sense, so in practice the PHP interface is almost 0, we do not have more than one interface definition code, if you put the interface code in a separate file, then open these files will take time, although it is very few.

By the way, I personally on the current development of PHP a little bit, PHP in order to adapt to enterprise development, adding a lot of new object-oriented features, a lot of things are directly from Java, such as interface, but the dynamic language really do not need interface this thing, such as type hints, in fact, is not very useful. Now PHP more and more complex, a bit nondescript, and gradually deviate from the people, to know that PHP can become the first language of web development, the main reason is the grassroots, simple, I hope that the development of PHP will be more and more good.

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