Search code examples
phpcomposer-phpphpstormautoloadpsr-0

'Alias is never used' PHPStorm error message within my PHP file


I am following the installation instructions here to install PHP RAML Parser

I run composer install and created the index.php below but it isn't working, I get an error:

Class 'Raml\ParseConfiguration' not found in /cygdrive/c/src/myapp/Raml/Parser.php on line 83

When I hover over the line use \Raml\Parser I get the PHPStorm warning message (Alias never used)

My index.php:

<?php
require ('Raml/Parser.php');
use \Raml\Parser; // Alias \Raml\Parser is never used
$parser = new \Raml\Parser();

Can anyone suggest what I've done wrong?


Solution

  • Provided that the file Raml/Parser.php contains:

    namespace Raml;
    
    class Parser {}
    

    You can either do this:

    require ('Raml/Parser.php');
    $parser = new \Raml\Parser();
    

    or this:

    require ('Raml/Parser.php');
    use \Raml\Parser;
    $parser = new Parser();
    

    use imports a class/interface/trait into your current namespace and allows to use a shorter name instead of the fully qualified, backspaced name. It also allows to switch to a different class by only changing the use statement, and not every name reference in the whole class, but this benefit is very small because using PHPStorm brings some powerful renaming abilities itself.