<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>

CC BY-SA 3.0
2018-03-19T20:50:58.477Z
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.
Dan Zuzevich
CC BY-SA 3.0
2018-03-20T11:35:24.200Z
Just a quick question. Wouldnt $criteria be, $this->criteria in the constructor? Just checking if this was intentional or not.
Dan Zuzevich
CC BY-SA 3.0
2018-03-20T11:41:04.673Z
It depends what you are going to do. I don't know what you want or achieve.
Robin Schambach
CC BY-SA 3.0
2018-03-20T11:46:57.287Z
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.
Dan Zuzevich
CC BY-SA 3.0
2018-03-20T11:52:24.007Z
Dont really know why I didnt just do it all in the Service.
Dan Zuzevich
CC BY-SA 3.0
2018-03-20T12:00:54.960Z
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
Robin Schambach
CC BY-SA 3.0
2018-03-20T12:04:43.830Z
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?
Dan Zuzevich
CC BY-SA 3.0
2018-03-20T12:19:51.243Z
13:18 so my day ends in about 11 hours. Take your time, I don't have anything else to do today
Robin Schambach
CC BY-SA 3.0
2018-03-20T13:25:36.320Z
https://gist.github.com/DZuz14/9d2c7869c2ecf7c25feec478417f6563
Dan Zuzevich
CC BY-SA 3.0
2018-03-20T13:39:03.247Z
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
Robin Schambach
CC BY-SA 3.0
2018-03-20T13:57:33.587Z
Nope I am not. That would be epic. Care to hop in a chat?
Dan Zuzevich
CC BY-SA 3.0
2018-03-20T14:03:01.590Z
Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/74808/discussion-between-robin-schambach-and-daniel-zuzevich).
Robin Schambach
CC BY-SA 3.0
2018-03-19T19:53:49.257Z
2018-03-19T19:59:06.440Z
2018-03-19T19:59:06.440Z
Robin Schambach
<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>

<plugin-development>
Pass Craft Instance to A Vendor PHP File
# 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-robin-schambach-and-daniel-zuzevich).  

---

