dongyisa6254 2014-11-02 22:09
浏览 30
已采纳

依赖注入:ctachable错误,参数1传递给* [关闭]

I didn't dependency injection. I read same questions but i can't.

index.php:

<?php
    namespace Test;

    use \Test\File\FileControl;
    require "b.php";

    $s = new FileControl;
    $s->taner($re);

b.php

<?php

namespace Test\File;
require "c.php";

class FileControl {

    public function taner(re $re) {
        $re->taner();
    }
}

c.php

namespace Test\File;

class re {

    public function taner() {
        echo 'hellow!';
    }
}

print: Catchable fatal error: Argument 1 passed to Test\File\FileControl::taner() must be an instance of Test\Filee, null given, called in /index.php on line 9 and defined in /b.php on line 8

what's the wrong, please help

  • 写回答

1条回答 默认 最新

  • dpthuyh1678 2014-11-02 22:11
    关注

    The error is pretty specific.. The argument you're passing is invalid, because the method is expecting a certain type of object, and you are passing in the equivalent of null instead.

    Edit: you need to instantiate a re object in index.php. So change this...

    <?php
        namespace Test;
    
        use \Test\File\FileControl;
        require "b.php";
    
        $s = new FileControl;
        $s->taner($re);
    

    to

    <?php
    namespace Test;
    
    use \Test\File\FileControl;
    require "b.php";
    
    $re = new re(); // here's the new line
    $s = new FileControl;
    $s->taner($re);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 obsidian的中文层级自动编号
  • ¥15 同一个网口一个电脑连接有网,另一个电脑连接没网
  • ¥15 神经网络模型一直不能上GPU
  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!
  • ¥20 wpf datagrid单元闪烁效果失灵
  • ¥15 券商软件上市公司信息获取问题
  • ¥100 ensp启动设备蓝屏,代码clock_watchdog_timeout
  • ¥15 Android studio AVD启动不了
  • ¥15 陆空双模式无人机怎么做
  • ¥15 想咨询点问题,与算法转换,负荷预测,数字孪生有关