What does the php interface consist of?

(*-*)浩
Release: 2023-02-27 15:12:01
Original
2775 people have browsed it

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
?>
Copy after login

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

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!

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