MikeLindsay
3 months ago
106
1
Laravel

Type Hinting and Method Injection in a Custom Class

Posted 3 months ago by MikeLindsay

Hi guys, just wrapping my head around the IOC container.

I want to inject a custom class into the method of another class. I don't want to put it in the constructor, I want to use it only in that method alone. I want to be able to bind it into the container in a service provider and not use any helper methods in my classes.

Can someone give me a steer on this??

EDIT - so I just discovered the app()->call('App\[email protected]'). Which works. Is there someway to put this or register this in a service provider so that I don't have to use the app() helper in my classes??

Thanks in advance

<?php

namespace App;

class Rabbit
{
    //The class to be injected
}

class Roger
{
    public function rabbit(Rabbit $rabbit)
    {
        //Have access to the Rabbit class here!
    }
}

Please sign in or create an account to participate in this conversation.