Home  >  Article  >  Backend Development  >  What does the php interface consist of?

What does the php interface consist of?

(*-*)浩
(*-*)浩Original
2019-10-21 10:50:482759browse

Mainly constrains and regulates the class name, the methods owned by the class, and the passed parameters. It feels a bit like the php abstract abstract class.

What does the php interface consist of?

1. Definition and calling of interface (Recommended learning: PHP video tutorial)

<?php
interface face1
{
const param = &#39;test&#39;;
public function show();
}
class test implements face1
{
public function show()
{
echo "interface is run<br>";
}
}
$face = new test();
echo $face->show();         //inerface is run
echo face1::param;           //test
?>

Note: One thing to note in the above example is that the method name of the interface is show, and the class that inherits the interface must have the show method, otherwise an error will be reported. In other words, the methods of the interface are fake, and what really works are the methods in the inherited class. Because of this, I think the interface is somewhat similar to the abstract class of PHP.

Second, the parameter constraints are relatively strict

<?php
interface face1
{
public function show(show $show);
}
// 显示正常
class test implements face1
{
public function show(show $show)
{
echo "asdfasdf";
}
}
// 报fatal错误
class test2 implements face1
{
public function show(aaa $aaa)
{
}
}
?>

Explanation: The above example reports a fatal error. Why does it report a fatal error? The reason is that the parameter passed is aaa $aaa, not show $show. In an interface class that inherits, when calling a method of the interface, the parameters passed must match the parameter names in the interface. Otherwise, an error will be reported.

The above is the detailed content of What does the php interface consist of?. 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