<p><code>craft()</code> is just a wrapper for the singleton instance of the <a href="https://craftcms.com/classreference/etc/web/WebApp" rel="noreferrer">WebApp</a> class and you can call its <a href="https://craftcms.com/classreference/etc/web/WebApp#public-methods" rel="noreferrer">methods directly</a> like you're doing with <code>getSiteUrl()</code>.</p>

<p>You'll notice that <code>getSiteUrl()</code> actually isn't in that list of WebApp methods, though.  That's because it (along with other methods) live in <a href="https://craftcms.com/classreference/etc/behaviors/AppBehavior" rel="noreferrer">AppBehavior</a>, which is a <a href="http://www.yiiframework.com/wiki/44/behaviors-events/" rel="noreferrer">Yii behavior</a> and is very similar to a <a href="https://secure.php.net/manual/en/language.oop5.traits.php" rel="noreferrer">trait</a> that gets it's methods "mixed-in" to the class that wants them (in this case, WebApp).</p>

<p>Stand-alone classes that are't attached to <code>WebApp</code> (<code>craft()</code>), like <a href="https://craftcms.com/classreference/etc/dates/DateTime" rel="noreferrer">DateTime</a>, you can instantiate like:</p>

<pre><code>$dateTime = new DateTime();
$dateTime-&gt;format(...);
</code></pre>

<p>Or if they are static classes (like the <a href="https://craftcms.com/classreference/#craft.app.helpers" rel="noreferrer">Helpers</a>), you can use directly like:</p>

<pre><code>$lowerCaseString = StringHelper::toLowerCase('ABC');
</code></pre>

CC BY-SA 3.0
2016-07-13T18:17:01.783Z
2016-07-13T18:17:01.783Z
Brad Bell
<p>I'm working with dates and want to use Craft's Class reference to speed things along but am struggling to figure out how to use them. For example, this works as expected and returns the siteUrl:</p>

<pre><code>return craft()-&gt;getSiteUrl();
</code></pre>

<p>But how do I format something more complicated? Say I want to format a date variable, how do I use any of these classes: <a href="https://craftcms.com/classreference/etc/dates/DateTime" rel="noreferrer">https://craftcms.com/classreference/etc/dates/DateTime</a>? I've tried all sorts but generally end up with:</p>

<pre><code>Craft\WebApp and its behaviors do not have a method or closure named...
</code></pre>

<plugin-development><classes>
How to access Craft classes within plugin?
# How to access Craft classes within plugin?

(Asked by: username_0 on 2016-07-13)

I'm working with dates and want to use Craft's Class reference to speed things along but am struggling to figure out how to use them. For example, this works as expected and returns the siteUrl:
return craft()->getSiteUrl();

But how do I format something more complicated? Say I want to format a date variable, how do I use any of these classes: https://craftcms.com/classreference/etc/dates/DateTime? I've tried all sorts but generally end up with:
Craft\WebApp and its behaviors do not have a method or closure named...

**Tags:** plugin-development, classes

---

## 1 answers

### Answer by username_1 on 2016-07-13. Score: 5 | Accepted answer

craft() is just a wrapper for the singleton instance of the WebApp class and you can call its methods directly like you're doing with getSiteUrl().
You'll notice that getSiteUrl() actually isn't in that list of WebApp methods, though.  That's because it (along with other methods) live in AppBehavior, which is a Yii behavior and is very similar to a trait that gets it's methods "mixed-in" to the class that wants them (in this case, WebApp).
Stand-alone classes that are't attached to WebApp (craft()), like DateTime, you can instantiate like:
$dateTime = new DateTime();
$dateTime->format(...);

Or if they are static classes (like the Helpers), you can use directly like:
$lowerCaseString = StringHelper::toLowerCase('ABC');


---

