Home > OS >  PhpStorm auto include used PHP files
PhpStorm auto include used PHP files

Time:05-04

I want PhpStorm to auto-include (include_once) PHP files that I use in my respective .php file.

Simple example: MyTestClass.php:

<?php

namespace Test;

class MyTestClass
{
    public function sayHello()
    {
        echo "hello world";
    }
}

When I use this class in my index.php file, only the namespace is auto inserted:

<?php

use Test\MyTestClass;

$test = new MyTestClass();
$test->sayHello();

But this does not work in PHP. I've to include the PHP file that contains the class:

<?php

include_once('./namesapce.php');

use Test\MyTestClass;

$test = new MyTestClass();
$test->sayHello();

Is it possible that PhpStorm inserts these files automatically with the help of include_once?

CodePudding user response:

Nope. PhpStorm cannot do that.

And it's extremely unlikely to be implemented.

The reason is simple: it's 2022 now and PHP has class autoloading functionality for many years (since PHP 5 which is 2004):


BTW: you can use Composer autoloader for this purpose. It's fast and easy to use.

Are you using custom libraries or frameworks? If yes then quite likely you have it installed using Composer already. It's a standard tool these days for installing PHP packages (managing your dependencies) and classes autoloading.

You just need to tell where to find your classes in composer.json file and require_once the actual autoloader.

https://getcomposer.org/doc/01-basic-usage.md#autoloading

  • Related