Leff
Leff

Reputation: 1350

Php - making a class instance that has dependencies in the constructor

I have a class, that has a constructor that looks like this:

use App\Libraries\Content\ContentInterface;
use EllipseSynergie\ApiResponse\Laravel\Response;

class ImportController extends Controller
{
    private $indexable;

    function __construct(Response $response, ContentInterface $contentInterface) {
        $this->indexable        = \Config::get('middleton.wp.content.indexable_types');
        $this->response         = $response;
        $this->contentInterface = $contentInterface;
    }

    public function all() {
        $include      = array_diff($this->indexable, ['folder']);
        $importResult = $this->import($include);
        $this->deleteOldContent($importResult['publishedPostsIDs']);

        return $importResult['imported'];
    }

How can I instantiate this class from another class and call the method all() from it? I have tried with something like this:

use EllipseSynergie\ApiResponse\Laravel\Response;
use App\Libraries\Content\ContentInterface;

class ContentImport extend Command {

public function handle() {

    (new ImportController(new Response, new ContentInterface))->all();
}

But, that doesn't work, I get the error that I should pass the arguments to the Response class too:

 [Symfony\Component\Debug\Exception\FatalThrowableError]                                                                    
  Type error: Too few arguments to function EllipseSynergie\ApiResponse\AbstractResponse::__construct(), 0 passed in /home/  
  vagrant/Projects/middleton/app/Console/Commands/ContentImport.php on line 43 and exactly 1 expected  

What is the correct way of doing this?

Upvotes: 1

Views: 64

Answers (1)

robbyrr
robbyrr

Reputation: 392

I believe this should work

use EllipseSynergie\ApiResponse\Laravel\Response;
use App\Libraries\Content\ContentInterface;

class ContentImport extend Command {

    public function handle(ImportController $importController) {

    $importController->all();
}

Upvotes: 1

Related Questions