0

With Symfony I'm taking the habit of using the class name resolver ::class (since php5.5):

use AppBundle\Entity\Product;

// ...

$resolver->setDefaults(array(
    'data_class' => Product::class
));

instead of the FQN string:

$resolver->setDefaults(array(
    'data_class' => 'AppBundle\Entity\Product'
));

I'm doing that for readability purpose and because it's handy to introscpect in some smart IDEs.

However, I'm seeing not so much examples of this practice in official docs. So I'm wondering if this is because of the compatibility concern (< php5.5) or because it would not be good to systematically import all the classes we need to reference.

My question is: Is it a good practice to use ::class everywhere we need to reference a FQN ?

Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307
  • as you said `is because of the compatibility concern (< php5.5)` – Matteo Jun 06 '16 at 09:44
  • Thank you for your comment. I don't see why my question would deserve a close and a -1 though – Pierre de LESPINAY Jun 06 '16 at 09:54
  • The "Good practices" are very often opinion based, but apart the Matteo's comment about compatibility (I agree with him), I think you should ask yourself why not use it. I see only advantages using the class name resolver, for example if you change the namespace of the class you are not forced to change it everywhere in the code (I use `::class` everywhere and not also in the Entities). – gp_sflover Jun 06 '16 at 15:22
  • I'd say _best practice_ is opinion based, _good practice_ here is more looking for eventual caveats about the practice, apart from the compatibility issue that I mentioned in the question. – Pierre de LESPINAY Jun 06 '16 at 17:38
  • 1
    I don't think this deserves a -1 either, so I did a +1. – Alvin Bunk Jun 06 '16 at 18:31

1 Answers1

0

The answer seems to be:

Apart from the compatibility issue with php<5.5 there is nothing that would prevent one to use the ::class name resolver everywhere we need.

Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307