<p>Here's how you would do that...</p>

<ol>
<li>Pass your parameter to the class:</li>
</ol>

<pre class="lang-php prettyprint-override"><code>'transformer' =&gt; new MyTransformerClassName($user),
</code></pre>

<ol start="2">
<li>Use a <a href="https://www.php.net/manual/en/language.oop5.decon.php" rel="nofollow noreferrer">constructor</a> to save it as a <a href="https://www.php.net/manual/en/language.oop5.properties.php" rel="nofollow noreferrer">property</a>:</li>
</ol>

<pre class="lang-php prettyprint-override"><code>class MyTransformerClassName extends TransformerAbstract
{
    private $user;

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

    public function transform(Entry $entry) ...
}
</code></pre>

<ol start="3">
<li>Use your property as needed:</li>
</ol>

<pre class="lang-php prettyprint-override"><code>class MyTransformerClassName extends TransformerAbstract
{
    private $user;

    public function __construct($user) ...


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

CC BY-SA 4.0
2019-11-25T17:14:55.197Z
2019-11-25T17:14:55.197Z
Jalen Davenport
<p>I have transformer like down below in the element API.</p>

<pre><code>'transformer' =&gt; 'MyTransformerClassName',
</code></pre>

<p>Or</p>

<pre><code>'transformer' =&gt; new MyTransformerClassName(),
</code></pre>

<p>I am going to pass a param like <code>$user</code> through <code>MyTransformerClassName</code> to use in separate Class Like here.</p>

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

<p>How can I pass the <code>$user</code> variable to the separate transformer class to use it in transform method?</p>

<craft3><plugin-elementapi><yii>
Pass params to transform function of Transformer class in split Transformer class
# Pass params to transform function of Transformer class in split Transformer class

(Asked by: username_0 on 2019-11-19)

I have transformer like down below in the element API.
'transformer' => 'MyTransformerClassName',

Or
'transformer' => 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?

**Tags:** craft3, plugin-elementapi, yii


---

## 1 answers

### Answer by username_1 on 2019-11-25. Score: 2

Here's how you would do that...

Pass your parameter to the class:

'transformer' => new MyTransformerClassName($user),


Use a constructor to save it as a property:

class MyTransformerClassName extends TransformerAbstract
{
    private $user;

    public function __construct($user)
    {
        $this->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' => $this->user,
            ...
        ];
    }
}


---

