Search code examples
phpclassrequire

Can I use the 'Use' operator from a different PHP file?


I have this code here which will be used quite a lot throughout my app:

require_once(APPPATH.'libraries/parse/autoload.php');

use Parse\ParseClient;
use Parse\ParseObject;
use Parse\ParseQuery;
use Parse\ParseException;
use Parse\ParseUser;
use Parse\ParseFile;
use Parse\ParseSessionStorage;
use Parse\ParseAnalytics;

I placed all of that code in a file called parse.php, however when I attempt to include that file use classes aren't being found.

Is there something I am doing wrong?

Here is the error I get:

Fatal error: Class 'ParseClient' not found in /xxx/xxx/xxx.php on line 11


Solution

  • use doesn't include anything. It just imports the specified namespace (or class) to the current scope. If you want the classes to be autoloaded - read about autoloading functionality of PHP(http://php.net/manual/en/language.oop5.autoload.php)