Pass params to transform function of Transformer class in split Transformer class I have transformer like down below in the element API.

'transformer' =&gt; 'MyTransformerClassName',


Or

'transformer' =&gt; new MyTransformerClassName(),


I am going to pass a param like $user through MyTransformerClassName to use in separate Class Like here.

class MyTransformerClassName extends TransformerAbstract
{
    public function transform(Entry $entry)
    {
        return [
            // ... here want to use $user coming from element api
        ];
    }
}


How can I pass the $user variable to the separate transformer class to use it in transform method?
Here's how you would do that...


Pass your parameter to the class:


'transformer' =&gt; new MyTransformerClassName($user),



Use a constructor to save it as a property:


class MyTransformerClassName extends TransformerAbstract
{
    private $user;

    public function __construct($user)
    {
        $this-&gt;user = $user;
    }

    public function transform(Entry $entry) ...
}



Use your property as needed:


class MyTransformerClassName extends TransformerAbstract
{
    private $user;

    public function __construct($user) ...


    public function transform(Entry $entry)
    {
        return [
            'user' =&gt; $this-&gt;user,
            ...
        ];
    }
}