dscqrkvr9562034621 2018-08-14 11:32
浏览 55

传递具体类而不是实现接口php的类

I can not figure our why I get this error: Argument 1 passed to App\src\Manager::manage() must be an instance of App\src\ManageableInterface, instance of App\src\Worker given. I Coded to the interface ManageableInterface not to the conrete class but it does not work. Here is the code:

ManageableInterface.php

<?php namespace App\src\interfaces;

interface ManageableInterface
{
  public function manage();
}

SleepableInterface.php

<?php namespace App\src\interfaces;

interface SleepableInterface
{
  public function sleep();
}

WorkableInterface.php

<?php namespace App\src\interfaces;

interface WorkableInterface
{
 public function work();
}

Worker.php

<?php namespace App\src;

use App\src\interfaces\{WorkableInterface, SleepableInterface, ManageableInterface};

class Worker implements WorkableInterface, SleepableInterface, ManageableInterface
{
  public function work()
  {
    var_dump('Worker is working.');
  }


  public function sleep()
  {
    var_dump('Worker is sleeping.');
  }


  public function manage()
  {
    $this->work();
    $this->sleep();
  }

}

Manager.php

<?php namespace App\src;

class Manager
{
  public function manage(ManageableInterface $worker)
  {
    $worker->manage();
  }
 }

index.php

<?php require 'vendor/autoload.php';

use App\src\{Manager, Worker};

$worker = new Worker();

$manager = new Manager();
$manager->manage($worker);
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM
    • ¥15 划分vlan后不通了
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)