Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-11-25T17:14:55.197Z
Answers.LastActivityDate: 2019-11-25T17:14:55.197Z
Answers.UserDisplayName: Jalen Davenport
Body: <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>
  
Tags: <craft3><plugin-elementapi><yii>
Title: Pass params to transform function of Transformer class in split Transformer class
ThreadText: # 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...
  
  1. Pass your parameter to the class:
  
  ```php
  'transformer' => new MyTransformerClassName($user),
  
  ```
  
  2. Use a [constructor](https://www.php.net/manual/en/language.oop5.decon.php) to save it as a [property](https://www.php.net/manual/en/language.oop5.properties.php):
  
  ```php
  class MyTransformerClassName extends TransformerAbstract
  {
      private $user;
  
      public function __construct($user)
      {
          $this->user = $user;
      }
  
      public function transform(Entry $entry) ...
  }
  
  ```
  
  3. Use your property as needed:
  
  ```php
  class MyTransformerClassName extends TransformerAbstract
  {
      private $user;
  
      public function __construct($user) ...
  
      public function transform(Entry $entry)
      {
          return [
              'user' => $this->user,
              ...
          ];
      }
  }
  
  ```
  
  
  ---
  
  