Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-13T18:17:01.783Z
Answers.LastActivityDate: 2016-07-13T18:17:01.783Z
Answers.UserDisplayName: Brad Bell
Body: <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>
  
Tags: <plugin-development><classes>
Title: How to access Craft classes within plugin?
ThreadText: # 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](https://craftcms.com/classreference/etc/web/WebApp) class and you can call its [methods directly](https://craftcms.com/classreference/etc/web/WebApp#public-methods) 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](https://craftcms.com/classreference/etc/behaviors/AppBehavior), which is a [Yii behavior](http://www.yiiframework.com/wiki/44/behaviors-events/) and is very similar to a [trait](https://secure.php.net/manual/en/language.oop5.traits.php) 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](https://craftcms.com/classreference/etc/dates/DateTime), you can instantiate like:
  
  ```
  $dateTime = new DateTime();
  $dateTime->format(...);
  
  ```
  
  Or if they are static classes (like the [Helpers](https://craftcms.com/classreference/#craft.app.helpers)), you can use directly like:
  
  ```
  $lowerCaseString = StringHelper::toLowerCase('ABC');
  
  ```
  
  
  ---
  
  