Home  >  Article  >  Backend Development  >  How to use php interface correctly

How to use php interface correctly

小云云
小云云Original
2018-02-24 14:38:112321browse

The use of interfaces can enable the development of less missing function codes. This article mainly shares with you examples of the use of php interfaces and methods implemented using the phpstrom tool. I hope it can help everyone.

1Create an interface file

game_interface.php

<?phpnamespace Home\Tool;;use Home\Tool;/**
 * Created by PhpStorm.
 * User: smile
 * Date: 2018/1/4
 * Time: 20:36
 */interface game_interface{
    public static function caiji();
    public static function js();
    public static function save();
    public static function check_guize();
    public static function fanshui();}

2, create a class to inherit this interface

Game.class.php

<?phpnamespace Home\Tool;;use Home\Tool;/**
 * Created by PhpStorm.
 * User: smile
 * Date: 2018/1/4
 * Time: 20:36
 */abstract class Game implements game_interface{}

3 The functional class then inherits Game

Pk10.class.php

<?phpnamespace Home\Tool;use Home\Tool;class Pk10 extends Game {
    public static $name ="北京赛车";    public static function js()
    {
        // TODO: Implement js() method.
        caiji();
    }    public static function caiji()
    {
        // TODO: Implement caiji() method.
    }    public static function save()
    {
        // TODO: Implement save() method.
    }    public static function check_guize()
    {
        // TODO: Implement check_guize() method.
    }    public static function fanshui()
    {
        // TODO: Implement fanshui() method.
    }
}

extends. When extending the class, the phpstrom tool will automatically generate the code, as shown above. If the above code is missing a method, an error will be prompted. Move the mouse to the game and hold down the alt + enter keys to add it automatically.

Related recommendations:

Sharing examples of multiple inheritance of PHP interfaces and tarits to implement multiple inheritance

Tips for using PHP interfaces

Some summaries on the use of PHP interfaces

The above is the detailed content of How to use php interface correctly. 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