0

I am trying to implement Dependency Injection in Symfony.

I created a class named Token as follows:

class Token
{
    private $token;
    private $key;

    public function __construct(string $key)
    {
        $this->key = $key;
        $this->settoken();
    }

    public function getToken(): string
    {
        return $this->token;
    }

    public function setToken(string $newString)
    {
        $token = $newString . '-' . $this->key;

        return $this;
    }

}

In the construct, I have a key which is defined in services.yml

Now I have injected this class into another controller like below.

$this->token->setToken('123456789');

dd($this->token->getToken())

But this is giving me 'Too few arguments to function setToken()' error. I think this is because in my Token class construct I have already passed key argument.

I am not sure how to properly use it.

Can anyone please help me.

Thank You.

Znk
  • 29
  • 7

1 Answers1

0

I see the problem in the Token class. If you look at the __construct, you are calling to $this->settoken()

public function __construct(string $key)
{
    $this->key = $key;
    $this->settoken();
}

And below is the setToken method that has a string as a mandatory argument. That's why it gives you an error.

The class should look like this :

class Token {

private $token;
private $key;

public function __construct(string $key)
{
    $this->key = $key;
}

public function getToken(): string
{
    return $this->token;
}

public function setToken(string $newString): Token
{
    $this->token = $newString . '-' . $this->key;

    return $this;
}

}

sefhi
  • 70
  • 1
  • 6