无法将类实例传递给构造函数

时间:2014-03-21 17:09:29

标签: php laravel-4

我已将此包https://github.com/Intervention/image与作曲家一起安装。我添加了

'IntImage' => 'Intervention\Image\Facades\Image'

在别名下配置/ app

我收到以下错误,无法弄清楚我做错了什么我确定它与命名空间/自动加载有关,但app / acme在composer.json的psr-o部分

  

'参数1传递给   Acme \ Services \ Images \ InterventionImageEditor :: __ construct()必须是   IntImage的实例,没有给出,调用   第14行/var/www/app/ACme/Providers/ImageEditorServiceProvider.php   并定义了'在   /var/www/app/Acme/Services/Images/InterventionImageEditor.php:11

我有以下目录结构

app
  acme
    Providers
      ImageEditorServiceProvider.php
    Services
      Images
        ImageEditorInterface.php
        InterventionImageEditor.php

和文件的内容

ImageEditorServiceProvider.php

<?php namespace Acme\Providers;

use Illuminate\Support\ServiceProvider;
use Acme\Services\Images\InterventionImageEditor;

/**
*
*/
class ImageEditorServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind('Acme\Services\Images\ImageEditorInterface', function () {
            return new InterventionImageEditor();
        });
    }
}

ImageEditorInterface.php

<?php namespace Acme\Services\Images;

interface ImageEditorInterface
{
    public function hello();
}

InterventionImageEditor.php

<?php namespace Acme\Services\Images;

use IntImage;

/**
*
*/
class InterventionImageEditor implements ImageEditorInterface
{
    protected $imageeditor;

    public function __construct(IntImage $imageeditor)
    {
        $this->imageeditor = $imageeditor;
    }

    public function hello()
    {
        $hello = 'hello';

        return $hello;
    }
}

我可以

吗?
Use IntImage;

以这种方式,因为它是一个立面或我错过了什么?

编辑以包含解决方案;

将服务提供商更改为以下解决了问题

<?php namespace Acme\Providers;

use Illuminate\Support\ServiceProvider;
use Acme\Services\Images\InterventionImageEditor;
use IntImage;

/**
*
*/
class ImageEditorServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind('Acme\Services\Images\ImageEditorInterface', function () {
            $intimage = new IntImage;
            return new InterventionImageEditor($intimage);
        });
    }
}

2 个答案:

答案 0 :(得分:1)

错误来自ImageEditorServiceProder.php

$this->app->bind('Acme\Services\Images\ImageEditorInterface', function () {
  return new InterventionImageEditor();
});

在这里,您要实例化InterventionImageEditor,不带任何参数。您InterventionImageEditor需要一个IntImage类型的参数。

如果您在实例化IntImage时有些地方没有InterventionImageEditor,那么您需要更新InterventionImageEditor::__construct以便它接受null(可能) 。

function __construct(IntImage $imageeditor = null)
{
    if (is_null($imageeditor)) {
        // Construct a default imageeditor
        // $imageeditor = new ...;
    }

    $this->imageeditor = $imageeditor;
}

答案 1 :(得分:1)

我不确定您是否可以使用IntImage,因为此文件是Facades。

如果您想扩展干预类。您应该将Intervention \ Image \ Image添加到ImageEditorServiceProvider。

use Intervention\Image\Image;

class ImageEditorServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind('Acme\Services\Images\ImageEditorInterface', function () {

            return new InterventionImageEditor(new Image);
        });
    }
}