Answers.Body: <p>The first one example should work as long as the last <code>BrowserDetection</code> segment maps to the actual <code>BrowserDetection.php</code> file on the file system.</p>
  
  <blockquote>
    <p>Fatal error: Class 'Craft\BrowserDetection' not found in /Volumes/Mark Notton/Clients/The STC/Website/craft/plugins/myPlugin/MyPlugin.php on line 33</p>
  </blockquote>
  
  <p>You'd still even get that error on the first one because the <code>BrowserDetection</code> class is in PHP's global namespace.</p>
  
  <p>You need to do:</p>
  
  <pre><code>$browser = new \BrowserDetection();
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-09T09:23:39.027Z
Answers.Text: Ah! It was the global namespace that was the problem. That little backslash resolved the problem. Thank you! I should mention though, the "Craft::import...." method still didn't work. "require_once..." worked perfectly.
Answers.UserDisplayName: Mark Notton
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-09T17:05:38.793Z
Answers.Text: Yeah, Yii 1 didn’t support PHP namespaces. We hacked things up a bit to support the Craft namespace via Craft::import(), but at the expense of requiring you to use \Yii::import() if you want to import a class in the global namespace. (Most of the time people are just loading dependencies via Composer, and require()'ing its autoloader script, so it hasn’t been much of an issue. Will be cleaner in Craft 3 though, which runs on Yii 2 which does support namespaces.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-09T02:17:02.270Z
Answers.LastActivityDate: 2015-10-09T02:26:40.687Z
Answers.LastEditDate: 2015-10-09T02:26:40.687Z
Answers.UserDisplayName: Brad Bell
Body: <p>I'm trying to include a PHP file (<a href="https://github.com/Wolfcast/BrowserDetection" rel="nofollow">BrowserDetection</a>) within my plugin.</p>
  
  <p>I've tried this: </p>
  
  <pre><code>Craft::import('plugins.myPlugin.resources.php.BrowserDetection');
  </code></pre>
  
  <p>Which doesn't return any errors, but also doesn't work. I've also tried:</p>
  
  <pre><code>require_once 'resources/php/BrowserDetection.php';
  </code></pre>
  
  <p>Which does pull in the file, but each time I tried to refer to it like: "$browser = new BrowserDetection();" I get something like this:</p>
  
  <blockquote>
    <p>Fatal error: Class 'Craft\BrowserDetection' not found in /Volumes/Mark
    Notton/Clients/The STC/Website/craft/plugins/myPlugin/MyPlugin.php on line
    33</p>
  </blockquote>
  
  <p>Apologies for the very noob question. I just don't understand how to pull in additional PHP resources. Perhaps I'm overlooking some bespoke craft syntax?</p>
  
Tags: <plugin-development><php>
Title: Include a PHP resource within a Plugin
ThreadText: # Include a PHP resource within a Plugin
  
  (Asked by: username_0 on 2015-10-09)
  
  I'm trying to include a PHP file ([BrowserDetection](https://github.com/Wolfcast/BrowserDetection)) within my plugin.
  
  I've tried this:
  
  ```
  Craft::import('plugins.myPlugin.resources.php.BrowserDetection');
  
  ```
  
  Which doesn't return any errors, but also doesn't work. I've also tried:
  
  ```
  require_once 'resources/php/BrowserDetection.php';
  
  ```
  
  Which does pull in the file, but each time I tried to refer to it like: "$browser = new BrowserDetection();" I get something like this:
  
  > Fatal error: Class 'Craft\BrowserDetection' not found in /Volumes/Mark
  > username_0/Clients/The STC/Website/craft/plugins/myPlugin/MyPlugin.php on line
  > 33
  
  Apologies for the very noob question. I just don't understand how to pull in additional PHP resources. Perhaps I'm overlooking some bespoke craft syntax?
  
  **Tags:** plugin-development, php
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-10-09. Score: 4 | Accepted answer
  
  The first one example should work as long as the last `BrowserDetection` segment maps to the actual `BrowserDetection.php` file on the file system.
  
  > Fatal error: Class 'Craft\BrowserDetection' not found in /Volumes/Mark username_0/Clients/The STC/Website/craft/plugins/myPlugin/MyPlugin.php on line 33
  
  You'd still even get that error on the first one because the `BrowserDetection` class is in PHP's global namespace.
  
  You need to do:
  
  ```
  $browser = new \BrowserDetection();
  
  ```
  
  > **Comments:**
  > **username_0**: Ah! It was the global namespace that was the problem. That little backslash resolved the problem. Thank you! I should mention though, the "Craft::import...." method still didn't work. "require_once..." worked perfectly.  
  > **username_2**: Yeah, Yii 1 didn’t support PHP namespaces. We hacked things up a bit to support the Craft namespace via Craft::import(), but at the expense of requiring you to use \Yii::import() if you want to import a class in the global namespace. (Most of the time people are just loading dependencies via Composer, and require()'ing its autoloader script, so it hasn’t been much of an issue. Will be cleaner in Craft 3 though, which runs on Yii 2 which does support namespaces.  
  
  ---
  
  