【发布时间】:2017-06-30 19:58:57
【问题描述】:
我正在尝试将接口作为构造函数参数传递
进入一个实现它并调用它的方法的类。
文件:growth.php
<?php
interface Growth {
public function growPlants():array;
}
文件:forest.php
<?php
class Forest implements Growth {
private $growth;
public function __construct(Growth $growth) {
$this->growth = $growth;
}
public function otherFunction():array {
$this->growth->growPlants();
}
}
文件:deepforest.php
<?php
class DeepForest implements Growth {
public function growPlants():array {
/* Actions */
}
}
文件:test.php
<?php
include "deepforest.php";
include "forest.php";
$deeforest = new DeepForest();
$forest = new Forest($deeforest);
未捕获的错误:调用未定义的方法 deepForest::otherFunction()
【问题讨论】:
标签: php class constructor interface implementation