Question: Inside my Controller I want a function to use `mpdf` e.g.

```
public function actionPdf(){

    include("MPDF57/mpdf.php");
    $mpdf=new mPDF('c'); 
    $mpdf->SetDisplayMode('fullpage');
    $mpdf->WriteHTML("<h1>Hello World!</h1>");
    $mpdf->Output('filename.pdf', 'F');

    }

}

```

This does not work, and throws an error: 

> 
> Class 'app\controllers\mPDF' not found
> 
> 
> 

What should I do If I want to autoload the class 

(a). Just for this Controller Action

(b). To make it usable everywhere just by using the `use` statement.

I know it has to do something with `namespaces` but don't know how do I define a namespace, and where do I place this `MPDF57` folder and then make it accessible.

I also tried this : 

```
 $name = "MPDF57/mpdf.php";
spl_autoload_register(function ($name) {
    var_dump($name);
});

```

But this didn't work either. throws the same error when I call my controller Action. 

Here is the namespace declaration and `use` statements inside : 

```
namespace app\controllers;

    use Yii;
    use app\models\Regs;
    use app\models\Voters;
    use app\models\RegsSearch;
    use yii\web\Controller;
    use yii\web\NotFoundHttpException;
    use yii\filters\VerbFilter;
    use \yii\web\Response;
    use yii\helpers\Html;
    use kartik\mpdf\Pdf;

```

Answer: 
Yii has already had autoloader, you do need nothing to load your class.
Just create your class with correct namespace and it will be loaded where are you using it only.

Namspace should represent real path to PHP file. PHP file name and class name should be same.
I've faced such problems in one of my previous projects. I'm not good at PHP or Yii2 - so follow my guide on your own risk :)

When you you add `use path\to\ExternalLibrary` that means the interface is ready to use inside current class (e.g. CurrentController.php). 

That means your application knows how to bring your path to it's stage. 

E.g. `use common\models\Post` lets you directly to use `Post` class, as `$posts = new Post;`

So if your library contains only one file, just put is some "canonic" path. To `common\models\` for example. So you can use it like any other model interface. 

But for sake of your project put it on vendor folder. Then install a random library with composer. And observe which files are modified (1-3 generally). Also try to understand the modification logic. When you get sure that you've grasped everything, copy and paste these parts and change the paths, names, etc. for your library. 

The best way, I think, is to make your library PSR-4 compatible and ship it as a PHP package. Thus, others can also benefit from your work.

There are lots of guides about making php packages. 

* <http://sitepoint.com/starting-new-php-package-right-way/>
* <https://knpuniversity.com/screencast/question-answer-day/create-composer-package>
* <http://jessesnet.com/development-notes/2015/create-php-composer-package/>
* <http://culttt.com/2014/03/12/build-php-package/>

If you are planning to be a good PHP developer, I recommend to look up Josh Lockhart's "Modern PHP: New Features and Good Practices" book ( free pdfs are available :) ). That will help you to understand the fundamentals of OO PHP including namespaces, interfaces etc. So, you will be able to handle such problems in modern way.