Answers.Body: <p>You don't need to pass anything in there.</p>
  
  <p>The function <code>craft()</code> is just a public function that returns the static app. It does not matter if you call it inside a plugin or somewhere else. </p>
  
  <pre><code>namespace Craft; //&lt;----- added namespace
  class Wally {
     function __construct() {
        $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
     }
  }
  </code></pre>
  
  <p>If you don't want to be in the same namespace you can do</p>
  
  <pre><code>class Wally {
     function __construct() {
        $criteria = \Craft\craft()-&gt;elements-&gt;getCriteria(\Craft\ElementType::Entry);
     }
  }
  </code></pre>
  
  <p>If you really want to save the app in an extra variable for whatever reasons you would do</p>
  
  <pre><code>new Wally(craft());
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-19T20:50:58.477Z
Answers.Text: Well, the particular reason is I wrote some Hackalicious code that is going to have to wait to be refactored. This will definitely work! Thanks a lot.
Answers.UserDisplayName: Dan Zuzevich
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-20T11:35:24.200Z
Answers.Text: Just a quick question. Wouldnt $criteria be, $this->criteria in the constructor? Just checking if this was intentional or not.
Answers.UserDisplayName: Dan Zuzevich
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-20T11:41:04.673Z
Answers.Text: It depends what you are going to do. I don't know what you want or achieve.
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-20T11:46:57.287Z
Answers.Text: Well, I ended up making an external class in a vendor file to handle a ton of the programming logic required in one of my Services files. The reason behind this was because I didn't want to clutter up my Service class file with a bunch of looping and traversal stuff. So basically, I queried my entry in the service, and ended up just passing that response off into my vendor class. However, I hit somewhat of a wall when I realized I needed to perform image transforms inside this class. I guess I was just trying to stay organized, but probably didnt go about it in the right way.
Answers.UserDisplayName: Dan Zuzevich
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-20T11:52:24.007Z
Answers.Text: Dont really know why I didnt just do it all in the Service.
Answers.UserDisplayName: Dan Zuzevich
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-20T12:00:54.960Z
Answers.Text: Maybe you could show me your class because generally speeking you would do it the same way like in the service no matter where your code is.in craft 3 everything is in the vendor folder
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-20T12:04:43.830Z
Answers.Text: Rofl. I am actually on the train rewriting it right now. I can share it in a bit. What time is it in Germany right now?
Answers.UserDisplayName: Dan Zuzevich
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-20T12:19:51.243Z
Answers.Text: 13:18 so my day ends in about 11 hours. Take your time, I don't have anything else to do today
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-20T13:25:36.320Z
Answers.Text: https://gist.github.com/DZuz14/9d2c7869c2ecf7c25feec478417f6563
Answers.UserDisplayName: Dan Zuzevich
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-20T13:39:03.247Z
Answers.Text: Before I say something else... Are you familiar with eager loading? Because there are quite a few (to be honest more than a few) parts in your code that is done by Craft. I could easily reduce your class by 80% which means less code, less queries and a better performance
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-20T13:57:33.587Z
Answers.Text: Nope I am not. That would be epic. Care to hop in a chat?
Answers.UserDisplayName: Dan Zuzevich
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-20T14:03:01.590Z
Answers.Text: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/74808/discussion-between-robin-schambach-and-daniel-zuzevich).
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-19T19:53:49.257Z
Answers.LastActivityDate: 2018-03-19T19:59:06.440Z
Answers.LastEditDate: 2018-03-19T19:59:06.440Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I created my own class file that I include in one of my Craft Services files. Is there any way I can access the craft instance inside of this file?</p>
  
  <p>For example, inside my Craft service I create a new instance of my class like:</p>
  
  <p>$wally = new Wally();</p>
  
  <p>Inside of the actual Wally class file, I'd like to be able to create a variable in my constructor that gives me access to stuff like this:</p>
  
  <pre><code>craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
  </code></pre>
  
  <p>For further clarification:</p>
  
  <pre><code>class Wally {
     function __construct($craftInstance) {
        $this-&gt;craft = $craftInstance;
     }
  }
  </code></pre>
  
  <p>If it sounds like I don't know what I'm doing, I probably don't. Any help would be appreciated. Using Craft 2.6 btw.</p>
  
Tags: <plugin-development>
Title: Pass Craft Instance to A Vendor PHP File
ThreadText: # Pass Craft Instance to A Vendor PHP File
  
  (Asked by: username_0 on 2018-03-19)
  
  I created my own class file that I include in one of my Craft Services files. Is there any way I can access the craft instance inside of this file?
  
  For example, inside my Craft service I create a new instance of my class like:
  
  $wally = new Wally();
  
  Inside of the actual Wally class file, I'd like to be able to create a variable in my constructor that gives me access to stuff like this:
  
  ```
  craft()->elements->getCriteria(ElementType::Entry);
  
  ```
  
  For further clarification:
  
  ```
  class Wally {
     function __construct($craftInstance) {
        $this->craft = $craftInstance;
     }
  }
  
  ```
  
  If it sounds like I don't know what I'm doing, I probably don't. Any help would be appreciated. Using Craft 2.6 btw.
  
  **Tags:** plugin-development
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-03-19. Score: 2 | Accepted answer
  
  You don't need to pass anything in there.
  
  The function `craft()` is just a public function that returns the static app. It does not matter if you call it inside a plugin or somewhere else.
  
  ```
  namespace Craft; //<----- added namespace
  class Wally {
     function __construct() {
        $criteria = craft()->elements->getCriteria(ElementType::Entry);
     }
  }
  
  ```
  
  If you don't want to be in the same namespace you can do
  
  ```
  class Wally {
     function __construct() {
        $criteria = \Craft\craft()->elements->getCriteria(\Craft\ElementType::Entry);
     }
  }
  
  ```
  
  If you really want to save the app in an extra variable for whatever reasons you would do
  
  ```
  new Wally(craft());
  
  ```
  
  > **Comments:**
  > **username_0**: Well, the particular reason is I wrote some Hackalicious code that is going to have to wait to be refactored. This will definitely work! Thanks a lot.  
  > **username_0**: Just a quick question. Wouldnt $criteria be, $this->criteria in the constructor? Just checking if this was intentional or not.  
  > **username_1**: It depends what you are going to do. I don't know what you want or achieve.  
  > **username_0**: Well, I ended up making an external class in a vendor file to handle a ton of the programming logic required in one of my Services files. The reason behind this was because I didn't want to clutter up my Service class file with a bunch of looping and traversal stuff. So basically, I queried my entry in the service, and ended up just passing that response off into my vendor class. However, I hit somewhat of a wall when I realized I needed to perform image transforms inside this class. I guess I was just trying to stay organized, but probably didnt go about it in the right way.  
  > **username_0**: Dont really know why I didnt just do it all in the Service.  
  > **username_1**: Maybe you could show me your class because generally speeking you would do it the same way like in the service no matter where your code is.in craft 3 everything is in the vendor folder  
  > **username_0**: Rofl. I am actually on the train rewriting it right now. I can share it in a bit. What time is it in Germany right now?  
  > **username_1**: 13:18 so my day ends in about 11 hours. Take your time, I don't have anything else to do today  
  > **username_0**: https://gist.github.com/DZuz14/9d2c7869c2ecf7c25feec478417f6563  
  > **username_1**: Before I say something else... Are you familiar with eager loading? Because there are quite a few (to be honest more than a few) parts in your code that is done by Craft. I could easily reduce your class by 80% which means less code, less queries and a better performance  
  > **username_0**: Nope I am not. That would be epic. Care to hop in a chat?  
  > **username_1**: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/74808/discussion-between-username_1-username_1-and-daniel-username_0).  
  
  ---
  
  