Home > Backend Development > PHP Tutorial > How to use php interface correctly

How to use php interface correctly

小云云
Release: 2023-03-20 12:14:01
Original
2406 people have browsed it

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();}
Copy after login

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{}
Copy after login

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.
    }
}
Copy after login

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!

Related labels:
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