Interface specification can be considered a template class, specifically how to run the operation still need to look at how to write a subclass inheritance, pay attention to the specific needs of local and wording as follows:
- Interface interface method must be public disclosure:
<?php
interface A{
public function info();
}
Wherein the public may be omitted, abbreviated as follows:
<?php
interface A{
function info();
}
If the definition of the type of method will report a fatal error: Access Method, interface of the type for A :: info () Omitted in /wwwroot/api.php the MUST BE ON Line 3 For example:
<?php
interface A{
private function info();
}
- It does not require an interface instantiation interface, but it needs to inherit implements, and the variable defined in the interface must be transmitted, inherited worded as follows:
<?php
interface A{
private function info($name);
}
class B implements A{
function info($name){
echo "Hello{$name}!";
}
}
$obj = new B();
$obj -> info('小桥');
- interface interface can define constants but does not allow the definition of member variables, such as:
<?php
class A {
const Num = 100;
}
No problem, but the definition of member variables as an error message appears: the Parse error: syntax error, Unexpected '$ NUM' (T_VARIABLE), expecting function (T_FUNCTION) or const (T_CONST) in /wwwroot/api.php ON Line 3 , for example, :
<?php
class A {
$num = 100;
}
- If a fatal error occurs when a subclass of non-method interfaces defined in the abstract class and no class: Fatal error: Access Method, interface of the type for A :: info () Omitted in /wwwroot/api.php the MUST BE ON Line 2 for example, :
<?php
interface A{
function info($name);
}
class B implements A{
function a($name){
echo "Hello{$name}!";
}
}
- interface function interface method is not recommended to define default values, if the subclass definition requires also defined, for example:
<?php
interface A{
function info($name = '小明');
}
class B implements A{
function info($name){
echo "Hello{$name}!";
}
}
Will appear Fatal error: Declaration of B :: info ( name = 'Bob') in /wwwroot/api.php on line 6fatal errors, even if you pass the info () method values, for example:
<?php
interface A{
function info($name = '小明');
}
class B implements A{
function info($name){
echo "Hello{$name}!";
}
}
$obj = new B();
$obj -> info('小明');
- Subclasses inherit multiple interfaces and can inherit an interface between:
//定义接口 A
interface A{
function info();
}
//定义接口 B 并继承接口 A
interface B extends A{
function eat($name, $food);
}
//定义子类 C 使用接口 B
class C implements B{
function info(){
echo '接口继承<br/>';
}
function eat($name = '小明', $food = '菜'){
echo $name . "正在吃" . $food . '<br>';
}
}
$demo = new C();
$demo -> info();
$demo -> eat();
$demo -> eat('小红', '苹果');