简体   繁体   中英

Can we implement interface in PHP class without using implements?

我的意思是我们可以直接在主类中使用接口类作为构造函数。

No, you can't instantiate an Interface in PHP.

<?php

interface TestError {
    public function test() {
        print "Bye!"; // Fatal error: Interface function Test::test() cannot contain body
    }
}

interface Test {
    public function test();
}

class Main {
    public function __construct () {
        $test = new Test(); // Fatal error: Cannot instantiate interface
    }
}

$main = new Main();

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM