2

I am trying to resolve class via __construct using Laravel's bind() method.

Here what I do:

routes.php (of course I will move it away from here)

// Bindings
App::bind(
    'License\Services\ModuleSelector\SelectorInterface',
    'License\Services\ModuleSelector\ModuleSelector'
);

SelectorInterface.php - interface that I will expect in __construct method.

<?php namespace License\Services\ModuleSelector;


interface SelectorInterface {

    /**
     * Simply return query that will select needle module fields
     *
     * @return mixed
     */
    public function make();

}

ModuleSelector.php - this is class that I want to resolve via Laravel's DI (see example below).

<?php namespace License\Services\ModuleSelector;    

use License\Services\ModuleSelector\Selector;   

class ModuleSelector extends Selector
{       
    /**
     * Get module by it's code
     *
     * @return mixed
     */
    public function find()
    {
        return $this->make()
            ->where('code', $module_code)
            ->first();
    }    
}

Module.php

<?php namespace License\Services\ModuleType;    

use License\Services\ModuleType\TypeInterface;
use License\Services\ModuleSelector\SelectorInterface;    

class Module 
{    
    ...    

    function __construct(SelectorInterface $selector)
    {
        $this->selector = $selector;
    }
    ...
}

And the place when error occurs:

In my repo I have use License\Services\ModuleType\Module as ModuleService;.

Than there is method called find():

/**
 * Find module by its code with all data (types, selected type)
 * @return mixed
 */
public function find($module_code)
{
    $module = new ModuleService;

    // Get module id in order to use build in relations in framework
    $module = $this->module->find($module_code);

    ...
}

So, in other words, I have 2 classes and one interface. What I am trying to do is:

1) Create Class1.php / Class2.php / Class2Interface.php.

2) In Class1.php in the __construct I specify __construct(Class2Interface $class2).

3) Instantiate Class2.

What I am doing wrong? Examples found here.

6
  • Of course not. I am trying to instansiate class by passing interface to the __construct using some kind of ReflectionClass. It is the basic functional from Laravel DI Commented Jul 21, 2014 at 9:11
  • What's the actual error you get? Commented Jul 21, 2014 at 9:12
  • Argument 1 passed to License\Services\ModuleType\Module::__construct() must be an instance of License\Services\ModuleSelector\SelectorInterface, none given Commented Jul 21, 2014 at 9:13
  • I don't see where are you trying to pass the instance? Commented Jul 21, 2014 at 9:21
  • I am not passing it at all. It is all about ReflectionClass and ioc container. As was said in documentation (laravel.com/docs/ioc#where-to-register) I am binding it here App::bind(...); Commented Jul 21, 2014 at 9:25

2 Answers 2

4

In this line:

$module = new ModuleService;

You are directly invoking the Module class and not passing in an instance of SelectorInterface.

For the IoC to work you bind and make classes using it. Try that line again with :

$module = App::make('License\Services\ModuleSelector\SelectorInterface');

An alernative is to inject it directly into your repos constructor, as long as the repo is created by the IoC container, your concrete will be automatically injected.

Sign up to request clarification or add additional context in comments.

2 Comments

Nope, it is not working. I think, you misunderstood me a little bit. The goal is to resolve ModuleSelector class via SelectorInterface (because ModuleSelector implements SelectorInterface). But Module class is just a simple php class that expects SelectorInterface in __construct(SelectorInterface $selector).
Yes, that is why you use the IoC to build the class and not invoke it manually with 'new'. I just re-read your comment to see if I misunderstood, but i don't think I have. To have automatic dependency resolution of any class that's dependencies are available, make it with the IoC container.
0

Nowhere do you have a class marked to actually "implement SelectorInterface".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.