8000 minor #7187 Use PHP 5.5's ::class notation everywhere (wouterj) · symfony/symfony-docs@85093fd · GitHub
[go: up one dir, main page]

Skip to content

Commit 85093fd

Browse files
committed
minor #7187 Use PHP 5.5's ::class notation everywhere (wouterj)
This PR was submitted for the 2.8 branch but it was merged into the 2.7 branch instead (closes #7187). Discussion ---------- Use PHP 5.5's ::class notation everywhere While updating to use the `::class` notation, I also fixed quite a few error's in the PHP code block, reformatted things a bit and updated some articles to use the AppBundle. /fixes #7186 Commits ------- 7892a6a Use PHP 5.5's ::class notation
2 parents 7d5ef77 + 7892a6a commit 85093fd

File tree

89 files changed

+610
-433
lines changed
  • console
  • controller
  • create_framework
  • doctrine
  • event_dispatcher
  • form
  • logging
  • profiler
  • reference
  • routing
  • security
  • serializer
  • service_container
  • session
  • setup
  • templating
  • testing
  • validation
  • Some content is hidden

    Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

    89 files changed

    +610
    -433
    lines changed

    _includes/service_container/_my_mailer.rst.inc

    Lines changed: 2 additions & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -27,9 +27,10 @@
    2727
    .. code-block:: php
    2828

    2929
    // app/config/services.php
    30+
    use AppBundle\Mailer;
    3031
    use Symfony\Component\DependencyInjection\Definition;
    3132

    3233
    $container->setDefinition('app.mailer', new Definition(
    33-
    'AppBundle\Mailer',
    34+
    Mailer::class,
    3435
    array('sendmail')
    3536
    ));

    best_practices/forms.rst

    Lines changed: 2 additions & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -19,6 +19,7 @@ form in its own PHP class::
    1919

    2020
    namespace AppBundle\Form;
    2121

    22+
    use AppBundle\Entity\Post;
    2223
    use Symfony\Component\Form\AbstractType;
    2324
    use Symfony\Component\Form\FormBuilderInterface;
    2425
    use Symfony\Component\OptionsResolver\OptionsResolver;
    @@ -39,7 +40,7 @@ form in its own PHP class::
    3940
    public function configureOptions(OptionsResolver $resolver)
    4041
    {
    4142
    $resolver->setDefaults(array(
    42-
    'data_class' => 'AppBundle\Entity\Post'
    43+
    'data_class' => Post::class,
    4344
    ));
    4445
    }
    4546

    bundles/extension.rst

    Lines changed: 5 additions & 2 deletions
    Original file line numberDiff line numberDiff line change
    @@ -137,14 +137,17 @@ Your bundles can also add their own classes into this file thanks to the
    137137
    ``addClassesToCompile()`` method. Define the classes to compile as an array of
    138138
    their fully qualified class names::
    139139

    140+
    use AppBundle\Manager\UserManager;
    141+
    use AppBundle\Utils\Slugger;
    142+
    140143
    // ...
    141144
    public function load(array $configs, ContainerBuilder $container)
    142145
    {
    143146
    // ...
    144147

    145148
    $this->addClassesToCompile(array(
    146-
    'AppBundle\\Manager\\UserManager',
    147-
    'AppBundle\\Utils\\Slugger',
    149+
    UserManager::class,
    150+
    Slugger::class,
    148151
    // ...
    149152
    ));
    150153
    }

    bundles/override.rst

    Lines changed: 2 additions & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -45,6 +45,7 @@ example, the implementing class for the ``original-service-id`` is changed to
    4545
    // src/Acme/DemoBundle/DependencyInjection/Compiler/OverrideServiceCompilerPass.php
    4646
    namespace Acme\DemoBundle\DependencyInjection\Compiler;
    4747

    48+
    use Acme\DemoBundle\YourService;
    4849
    use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
    4950
    use Symfony\Component\DependencyInjection\ContainerBuilder;
    5051

    @@ -53,7 +54,7 @@ example, the implementing class for the ``original-service-id`` is changed to
    5354
    public function process(ContainerBuilder $container)
    5455
    {
    5556
    $definition = $container->getDefinition('original-service-id');
    56-
    $definition->setClass('Acme\DemoBundle\YourService');
    57+
    $definition->setClass(YourService::class);
    5758
    }
    5859
    }
    5960

    components/class_loader/class_loader.rst

    Lines changed: 4 additions & 4 deletions
    Original file line numberDiff line numberDiff line change
    @@ -59,10 +59,10 @@ looked for in a location list to ease the vendoring of a sub-set of classes
    5959
    for large projects::
    6060

    6161
    $loader->addPrefixes(array(
    62-
    'Doctrine\\Common' => __DIR__.'/vendor/doctrine/common/lib',
    63-
    'Doctrine\\DBAL\\Migrations' => __DIR__.'/vendor/doctrine/migrations/lib',
    64-
    'Doctrine\\DBAL' => __DIR__.'/vendor/doctrine/dbal/lib',
    65-
    'Doctrine' => __DIR__.'/vendor/doctrine/orm/lib',
    62+
    'Doctrine\Common' => __DIR__.'/vendor/doctrine/common/lib',
    63+
    'Doctrine\DBAL\Migrations' => __DIR__.'/vendor/doctrine/migrations/lib',
    64+
    'Doctrine\DBAL' => __DIR__.'/vendor/doctrine/dbal/lib',
    65+
    'Doctrine' => __DIR__.'/vendor/doctrine/orm/lib',
    6666
    ));
    6767

    6868
    In this example, if you try to use a class in the ``Doctrine\Common`` namespace

    components/class_loader/psr4_class_loader.rst

    Lines changed: 2 additions & 4 deletions
    Original file line numberDiff line numberDiff line change
    @@ -38,17 +38,15 @@ The directory structure will look like this:
    3838
    demo.php
    3939
    4040
    In ``demo.php`` you are going to parse the ``config.yml`` file. To do that, you
    41-
    first need to configure the ``Psr4ClassLoader``:
    42-
    43-
    .. code-block:: php
    41+
    first need to configure the ``Psr4ClassLoader``::
    4442

    4543
    use Symfony\Component\ClassLoader\Psr4ClassLoader;
    4644
    use Symfony\Component\Yaml\Yaml;
    4745

    4846
    require __DIR__.'/lib/ClassLoader/Psr4ClassLoader.php';
    4947

    5048
    $loader = new Psr4ClassLoader();
    51-
    $loader->addPrefix('Symfony\\Component\\Yaml\\', __DIR__.'/lib/Yaml');
    49+
    $loader->addPrefix('Symfony\Component\Yaml\\', __DIR__.'/lib/Yaml');
    5250
    $loader->register();
    5351

    5452
    $data = Yaml::parse(file_get_contents(__DIR__.'/config.yml'));

    components/event_dispatcher.rst

    Lines changed: 4 additions & 3 deletions
    Original file line numberDiff line numberDiff line change
    @@ -201,27 +201,28 @@ determine which instance is passed.
    201201
    use Symfony\Component\DependencyInjection\Definition;
    202202
    use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
    203203
    use Symfony\Component\DependencyInjection\Reference;
    204+
    use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
    204205
    use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
    205206

    206207
    $containerBuilder = new ContainerBuilder(new ParameterBag());
    207208
    $containerBuilder->addCompilerPass(new RegisterListenersPass());
    208209

    209210
    // register the event dispatcher service
    210211
    $containerBuilder->setDefinition('event_dispatcher', new Definition(
    211-
    'Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher',
    212+
    ContainerAwareEventDispatcher::class,
    212213
    array(new Reference('service_container'))
    213214
    ));
    214215

    215216
    // register your event listener service
    216-
    $listener = new Definition('AcmeListener');
    217+
    $listener = new Definition(\AcmeListener::class);
    217218
    $listener->addTag('kernel.event_listener', array(
    218219
    'event' => 'foo.action',
    219220
    'method' => 'onFooAction',
    220221
    ));
    221222
    $containerBuilder->setDefinition('listener_service_id', $listener);
    222223

    223224
    // register an event subscriber
    224-
    $subscriber = new Definition('AcmeSubscriber');
    225+
    $subscriber = new Definition(\AcmeSubscriber::class);
    225226
    $subscriber->addTag('kernel.event_subscriber');
    226227
    $containerBuilder->setDefinition('subscriber_service_id', $subscriber);
    227228

    components/form.rst

    Lines changed: 2 additions & 2 deletions
    Original file line numberDiff line numberDiff line change
    @@ -621,7 +621,7 @@ option when building each field:
    621621
    ->add('dueDate', 'date', array(
    622622
    'constraints' => array(
    623623
    new NotBlank(),
    624-
    new Type('\DateTime'),
    624+
    new Type(\DateTime::class),
    625625
    )
    626626
    ))
    627627
    ->getForm();
    @@ -638,7 +638,7 @@ option when building each field:
    638638
    ->add('dueDate', 'date', array(
    639639
    'constraints' => array(
    640640
    new NotBlank(),
    641-
    new Type('\DateTime'),
    641+
    new Type(\DateTime::class),
    642642
    )
    643643
    ))
    644644
    ->getForm();

    components/security/authentication.rst

    Lines changed: 4 additions & 4 deletions
    Original file line numberDiff line numberDiff line change
    @@ -177,19 +177,19 @@ user. This allows you to use different encoding strategies for different
    177177
    types of users. The default :class:`Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactory`
    178178
    receives an array of encoders::
    179179

    180+
    use Acme\Entity\LegacyUser;
    180181
    use Symfony\Component\Security\Core\Encoder\EncoderFactory;
    181182
    use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
    183+
    use Symfony\Component\Security\Core\User\User;
    182184

    183185
    $defaultEncoder = new MessageDigestPasswordEncoder('sha512', true, 5000);
    184186
    $weakEncoder = new MessageDigestPasswordEncoder('md5', true, 1);
    185187

    186188
    $encoders = array(
    187-
    'Symfony\\Component\\Security\\Core\\User\\User' => $defaultEncoder,
    188-
    'Acme\\Entity\\LegacyUser' => $weakEncoder,
    189-
    189 10000 +
    User::class => $defaultEncoder,
    190+
    LegacyUser::class => $weakEncoder,
    190191
    // ...
    191192
    );
    192-
    193193
    $encoderFactory = new EncoderFactory($encoders);
    194194

    195195
    Each encoder should implement :class:`Symfony\\Component\\Security\\Core\\Encoder\\PasswordEncoderInterface`

    components/security/authorization.rst

    Lines changed: 4 additions & 2 deletions
    Original file line numberDiff line numberDiff line change
    @@ -118,9 +118,11 @@ on a "remember-me" cookie, or even authenticated anonymously?
    118118
    .. code-block:: php
    119119
    120120
    use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolver;
    121+
    use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
    122+
    use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
    121123
    122-
    $anonymousClass = 'Symfony\Component\Security\Core\Authentication\Token\AnonymousToken';
    123-
    $rememberMeClass = 'Symfony\Component\Security\Core\Authentication\Token\RememberMeToken';
    124+
    $anonymousClass = AnonymousToken::class;
    125+
    $rememberMeClass = RememberMeToken::Class;
    124126
    125127
    $trustResolver = new AuthenticationTrustResolver($anonymousClass, $rememberMeClass);
    126128

    components/serializer.rst

    Lines changed: 6 additions & 3 deletions
    Original file line numberDiff line numberDiff line change
    @@ -133,6 +133,8 @@ Deserializing an Object
    133133
    You'll now learn how to do the exact opposite. This time, the information
    134134
    of the ``Person`` class would be encoded in XML format::
    135135

    136+
    use Acme\Person;
    137+
    136138
    $data = <<<EOF
    137139
    <person>
    138140
    <name>foo</name>
    @@ -141,7 +143,7 @@ of the ``Person`` class would be encoded in XML format::
    141143
    </person>
    142144
    EOF;
    143145

    144-
    $person = $serializer->deserialize($data, 'Acme\Person', 'xml');
    146+
    $person = $serializer->deserialize($data, Person::class, 'xml');
    145147

    146148
    In this case, :method:`Symfony\\Component\\Serializer\\Serializer::deserialize`
    147149
    needs three parameters:
    @@ -155,7 +157,8 @@ Deserializing in an Existing Object
    155157

    156158
    The serializer can also be used to update an existing object::
    157159

    158-
    $person = new Acme\Person();
    160+
    // ...
    161+
    $person = new Person();
    159162
    $person->setName('bar');
    160163
    $person->setAge(99);
    161164
    $person->setSportsman(true);
    @@ -167,7 +170,7 @@ The serializer can also be used to update an existing object::
    167170
    </person>
    168171
    EOF;
    169172

    170-
    $serializer->deserialize($data, 'Acme\Person', 'xml', array('object_to_populate' => $person));
    173+
    $serializer->deserialize($data, Person::class, 'xml', array('object_to_populate' => $person));
    171174
    // $person = Acme\Person(name: 'foo', age: '69', sportsman: true)
    172175

    173176
    This is a common need when working with an ORM.

    console/commands_as_services.rst

    Lines changed: 6 additions & 8 deletions
    Original file line numberDiff line numberDiff line change
    @@ -52,11 +52,10 @@ with ``console.command``:
    5252
    .. code-block:: php
    5353
    5454
    // app/config/config.php
    55+
    use AppBundle\Command\MyCommand;
    56+
    5557
    $container
    56-
    ->register(
    57-
    'app.command.my_command',
    58-
    'AppBundle\Command\MyCommand'
    59-
    )
    58+
    ->register('app.command.my_command', MyCommand::class)
    6059
    ->addTag('console.command')
    6160
    ;
    6261
    @@ -164,13 +163,12 @@ inject the ``command.default_name`` parameter:
    164163
    .. code-block:: php
    165164
    166165
    // app/config/config.php
    166+
    use AppBundle\Command\MyCommand;
    167+
    167168
    $container->setParameter('command.default_name', 'Javier');
    168169
    169170
    $container
    170-
    ->register(
    171-
    'app.command.my_command',
    172-
    'AppBundle\Command\MyCommand',
    173-
    )
    171+
    ->register('app.command.my_command', MyCommand::class)
    174172
    ->setArguments(array('%command.default_name%'))
    175173
    ->addTag('console.command')
    176174
    ;

    console/logging.rst

    Lines changed: 4 additions & 2 deletions
    Original file line numberDiff line numberDiff line change
    @@ -109,11 +109,12 @@ First configure a listener for console exception events in the service container
    109109
    .. code-block:: php
    110110
    111111
    // app/config/services.php
    112+
    use AppBundle\EventListener\ConsoleExceptionListener;
    112113
    use Symfony\Component\DependencyInjection\Definition;
    113114
    use Symfony\Component\DependencyInjection\Reference;
    114115
    115116
    $definitionConsoleExceptionListener = new Definition(
    116-
    'AppBundle\EventListener\ConsoleExceptionListener',
    117+
    ConsoleExceptionListener::class,
    117118
    array(new Reference('logger'))
    118119
    );
    119120
    $definitionConsoleExceptionListener->addTag(
    @@ -206,11 +207,12 @@ First configure a listener for console terminate events in the service container
    206207
    .. code-block:: php
    207208
    208209
    // app/config/services.php
    210+
    use AppBundle\EventListener\ErrorLoggerListener;
    209211
    use Symfony\Component\DependencyInjection\Definition;
    210212
    use Symfony\Component\DependencyInjection\Reference;
    211213
    212214
    $definitionErrorLoggerListener = new Definition(
    213-
    'AppBundle\EventListener\ErrorLoggerListener',
    215+
    ErrorLoggerListener::class,
    214216
    array(new Reference('logger'))
    215217
    );
    216218
    $definitionErrorLoggerListener->addTag(

    controller/error_pages.rst

    Lines changed: 2 additions & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -317,10 +317,11 @@ In that case, you might want to override one or both of the ``showAction()`` and
    317317
    .. code-block:: php
    318318
    319319
    // app/config/services.php
    320+
    use AppBundle\Controller\CustomExceptionController;
    320321
    use Symfony\Component\DependencyInjection\Reference;
    321322
    use Symfony\Component\DependencyInjection\Definition;
    322323
    323-
    $definition = new Definition('AppBundle\Controller\CustomExceptionController', array(
    324+
    $definition = new Definition(CustomExceptionController::class, array(
    324325
    new Reference('twig'),
    325326
    '%kernel.debug%'
    326327
    ));

    controller/service.rst

    Lines changed: 4 additions & 5 deletions
    Original file line numberDiff line numberDiff line change
    @@ -79,11 +79,9 @@ Then you can define it as a service as follows:
    7979
    .. code-block:: php
    8080
    8181
    // app/config/services.php
    82-
    use Symfony\Component\DependencyInjection\Definition;
    82+
    use AppBundle\Controller\HelloController;
    8383
    84-
    $container->setDefinition('app.hello_controller', new Definition(
    85-
    'AppBundle\Controller\HelloController'
    86-
    ));
    84+
    $container->register('app.hello_controller', HelloController::class);
    8785
    8886
    Referring to the Service
    8987
    ------------------------
    @@ -230,11 +228,12 @@ argument:
    230228
    .. code-block:: php
    231229
    232230
    // app/config/services.php
    231+
    use AppBundle\Controller\HelloController;
    233232
    use Symfony\Component\DependencyInjection\Definition;
    234233
    use Symfony\Component\DependencyInjection\Reference;
    235234
    236235
    $container->setDefinition('app.hello_controller', new Definition(
    237-
    'AppBundle\Controller\HelloController',
    236+
    HelloController::class,
    238237
    array(new Reference('templating'))
    239238
    ));
    240239

    controller/soap_web_service.rst

    Lines changed: 3 additions & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -77,8 +77,10 @@ a ``HelloService`` object properly:
    7777
    .. code-block:: php
    7878
    7979
    // app/config/services.php
    80+
    use Acme\SoapBundle\Services\HelloService;
    81+
    8082
    $container
    81-
    ->register('hello_service', 'Acme\SoapBundle\Services\HelloService')
    83+
    ->register('hello_service', HelloService::class)
    8284
    ->addArgument(new Reference('mailer'));
    8385
    8486
    Below is an example of a controller that is capable of handling a SOAP

    controller/upload_file.rst

    Lines changed: 7 additions & 3 deletions
    Original file line numberDiff line numberDiff line change
    @@ -54,6 +54,7 @@ Then, add a new ``brochure`` field to the form that manages the ``Product`` enti
    5454
    // src/AppBundle/Form/ProductType.php
    5555
    namespace AppBundle\Form;
    5656

    57+
    use AppBundle\Entity\Product;
    5758
    use Symfony\Component\Form\AbstractType;
    5859
    use Symfony\Component\Form\FormBuilderInterface;
    5960
    use Symfony\Component\OptionsResolver\OptionsResolver;
    @@ -72,7 +73,7 @@ Then, add a new ``brochure`` field to the form that manages the ``Product`` enti
    7273
    public function configureOptions(OptionsResolver $resolver)
    7374
    {
    7475
    $resolver->setDefaults(array(
    75-
    'data_class' => 'AppBundle\Entity\Product',
    76+
    'data_class' => Product::class,
    7677
    ));
    7778
    }
    7879

    @@ -280,11 +281,12 @@ Then, define a service for this class:
    280281
    .. code-block:: php
    281282
    282283
    // app/config/services.php
    284+
    use AppBundle\FileUploader;
    283285
    use Symfony\Component\DependencyInjection\Definition;
    284286
    285287
    // ...
    286288
    $container->setDefinition('app.brochure_uploader', new Definition(
    287-
    'AppBundle\FileUploader',
    289+
    FileUploader::class,
    288290
    array('%brochures_directory%')
    289291
    ));
    290292
    @@ -407,11 +409,13 @@ Now, register this class as a Doctrine listener:
    407409
    .. code-block:: php
    408410
    409411
    // app/config/services.php
    412+
    use AppBundle\EventListener\BrochureUploaderListener;
    413+
    use Symfony\Component\DependencyInjection\Definition;
    410414
    use Symfony\Component\DependencyInjection\Reference;
    411415
    412416
    // ...
    413417
    $definition = new Definition(
    414-
    'AppBundle\EventListener\BrochureUploaderListener',
    418+
    BrochureUploaderListener::class,
    415419
    array(new Reference('brochures_directory'))
    416420
    );
    417421
    $definition->addTag('doctrine.event_listener', array(

    0 commit comments

    Comments
     (0)
    0