wesamly
wesamly

Reputation: 1584

leaving unused use statements in php

In symfony2 when I use console to generate a controller, symfony2 imports some classes using use statement, which sometimes I don't use in the actual code, for example:

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

what is the impact of leaving them, should I delete them.

I just don't tend to delete them, in case I needed them in later phase.

Upvotes: 1

Views: 1490

Answers (1)

Igor Pantović
Igor Pantović

Reputation: 9246

Having unused use statements in PHP doesn't impact performance. At least not in any noticeable measure.

Two statements you mention, however are used. They are used for annotation routing, eg:

<?php

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;


class MyController
{
    /**
     * @Route("/some/route")
     */
    public function someAction()
    {
        // ...
    }
} 

So if you are using them in annotations, you'll need them. If you aren't you can safely remove them.

You can technically use annotations without those use statements too but it looks terribly ugly:

<?php

class MyController
{
    /**
     * @Sensio\Bundle\FrameworkExtraBundle\Configuration\Route("/some/route")
     */
    public function someAction()
    {
        // ...
    }
} 

If you are using PHPStorm

If you are using PHPStorm IDE, you can install PHP Annotations plugin. It will recognize them and mark those two statements as used if you use them through annotations.

Upvotes: 8

Related Questions