Skip to content

Use PHP 5.5's ::class notation everywhere #7187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion _includes/service_container/_my_mailer.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
.. code-block:: php

// app/config/services.php
use AppBundle\Mailer;
use Symfony\Component\DependencyInjection\Definition;

$container->setDefinition('app.mailer', new Definition(
'AppBundle\Mailer',
Mailer::class,
array('sendmail')
));
3 changes: 2 additions & 1 deletion best_practices/forms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ form in its own PHP class::

namespace AppBundle\Form;

use AppBundle\Entity\Post;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand All @@ -42,7 +43,7 @@ form in its own PHP class::
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Post'
'data_class' => Post::class,
));
}
}
Expand Down
7 changes: 5 additions & 2 deletions bundles/extension.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,17 @@ Your bundles can also add their own classes into this file thanks to the
``addClassesToCompile()`` method. Define the classes to compile as an array of
their fully qualified class names::

use AppBundle\Manager\UserManager;
use AppBundle\Utils\Slugger;

// ...
public function load(array $configs, ContainerBuilder $container)
{
// ...

$this->addClassesToCompile(array(
'AppBundle\\Manager\\UserManager',
'AppBundle\\Utils\\Slugger',
UserManager::class,
Slugger::class,
// ...
));
}
Expand Down
3 changes: 2 additions & 1 deletion bundles/override.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ example, the implementing class for the ``original-service-id`` is changed to
// src/Acme/DemoBundle/DependencyInjection/Compiler/OverrideServiceCompilerPass.php
namespace Acme\DemoBundle\DependencyInjection\Compiler;

use Acme\DemoBundle\YourService;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

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

Expand Down
8 changes: 4 additions & 4 deletions components/class_loader/class_loader.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ looked for in a location list to ease the vendoring of a sub-set of classes
for large projects::

$loader->addPrefixes(array(
'Doctrine\\Common' => __DIR__.'/vendor/doctrine/common/lib',
'Doctrine\\DBAL\\Migrations' => __DIR__.'/vendor/doctrine/migrations/lib',
'Doctrine\\DBAL' => __DIR__.'/vendor/doctrine/dbal/lib',
'Doctrine' => __DIR__.'/vendor/doctrine/orm/lib',
'Doctrine\Common' => __DIR__.'/vendor/doctrine/common/lib',
'Doctrine\DBAL\Migrations' => __DIR__.'/vendor/doctrine/migrations/lib',
'Doctrine\DBAL' => __DIR__.'/vendor/doctrine/dbal/lib',
'Doctrine' => __DIR__.'/vendor/doctrine/orm/lib',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

decided not to update this one, as these are not real classes. It does work in PHP, but it can confuse people.

));

In this example, if you try to use a class in the ``Doctrine\Common`` namespace
Expand Down
6 changes: 2 additions & 4 deletions components/class_loader/psr4_class_loader.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,15 @@ The directory structure will look like this:
demo.php

In ``demo.php`` you are going to parse the ``config.yml`` file. To do that, you
first need to configure the ``Psr4ClassLoader``:

.. code-block:: php
first need to configure the ``Psr4ClassLoader``::

use Symfony\Component\ClassLoader\Psr4ClassLoader;
use Symfony\Component\Yaml\Yaml;

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

$loader = new Psr4ClassLoader();
$loader->addPrefix('Symfony\\Component\\Yaml\\', __DIR__.'/lib/Yaml');
$loader->addPrefix('Symfony\Component\Yaml\\', __DIR__.'/lib/Yaml');
$loader->register();

$data = Yaml::parse(file_get_contents(__DIR__.'/config.yml'));
Expand Down
34 changes: 18 additions & 16 deletions components/dependency_injection/autowiring.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ service is marked as autowired:

.. code-block:: php

use Acme\TwitterClient;
use Symfony\Component\DependencyInjection\Definition;

// ...
$definition = new Definition('Acme\TwitterClient');
$definition = new Definition(TwitterClient::class);
$definition->setAutowired(true);

$container->setDefinition('twitter_client', $definition);
Expand Down Expand Up @@ -150,9 +151,8 @@ modifying the class depending of them.

To follow this best practice, constructor arguments must be typehinted with interfaces
and not concrete classes. It allows to replace easily the current implementation
if necessary. It also allows to use other transformers.

Let's introduce a ``TransformerInterface``::
if necessary. It also allows to use other transformers. You can create a
``TransformerInterface`` containing just one method (``transform()``)::

namespace Acme;

Expand All @@ -164,18 +164,15 @@ Let's introduce a ``TransformerInterface``::
Then edit ``Rot13Transformer`` to make it implementing the new interface::

// ...

class Rot13Transformer implements TransformerInterface

// ...

{
// ...
}

And update ``TwitterClient`` to depend of this new interface::

class TwitterClient
{
// ...

public function __construct(TransformerInterface $transformer)
{
// ...
Expand Down Expand Up @@ -215,12 +212,13 @@ subsystem isn't able to find itself the interface implementation to register:

.. code-block:: php

use Acme\TwitterClient;
use Symfony\Component\DependencyInjection\Definition;

// ...
$container->register('rot13_transformer', 'Acme\Rot13Transformer');

$clientDefinition = new Definition('Acme\TwitterClient');
$clientDefinition = new Definition(TwitterClient::class);
$clientDefinition->setAutowired(true);
$container->setDefinition('twitter_client', $clientDefinition);

Expand Down Expand Up @@ -353,23 +351,27 @@ and a Twitter client using it:

.. code-block:: php

use Acme\Rot13Transformer;
use Acme\TransformerInterface;
use Acme\TwitterClient;
use Acme\UppercaseTransformer;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Definition;

// ...
$rot13Definition = new Definition('Acme\Rot13Transformer');
$rot13Definition->setAutowiringTypes(array('Acme\TransformerInterface'));
$rot13Definition = new Definition(Rot13Transformer::class);
$rot13Definition->setAutowiringTypes(array(TransformerInterface::class));
$container->setDefinition('rot13_transformer', $rot13Definition);

$clientDefinition = new Definition('Acme\TwitterClient');
$clientDefinition = new Definition(TwitterClient::class);
$clientDefinition->setAutowired(true);
$container->setDefinition('twitter_client', $clientDefinition);

$uppercaseDefinition = new Definition('Acme\UppercaseTransformer');
$uppercaseDefinition = new Definition(UppercaseTransformer::class);
$uppercaseDefinition->setAutowired(true);
$container->setDefinition('uppercase_transformer', $uppercaseDefinition);

$uppercaseClientDefinition = new Definition('Acme\TwitterClient', array(
$uppercaseClientDefinition = new Definition(TwitterClient::class, array(
new Reference('uppercase_transformer'),
));
$container->setDefinition('uppercase_twitter_client', $uppercaseClientDefinition);
Expand Down
7 changes: 4 additions & 3 deletions components/event_dispatcher.rst
Original file line number Diff line number Diff line change
Expand Up @@ -201,27 +201,28 @@ determine which instance is passed.
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;

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

// register the event dispatcher service
$containerBuilder->setDefinition('event_dispatcher', new Definition(
'Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher',
ContainerAwareEventDispatcher::class,
array(new Reference('service_container'))
));

// register your event listener service
$listener = new Definition('AcmeListener');
$listener = new Definition(\AcmeListener::class);
$listener->addTag('kernel.event_listener', array(
'event' => 'foo.action',
'method' => 'onFooAction',
));
$containerBuilder->setDefinition('listener_service_id', $listener);

// register an event subscriber
$subscriber = new Definition('AcmeSubscriber');
$subscriber = new Definition(\AcmeSubscriber::class);
$subscriber->addTag('kernel.event_subscriber');
$containerBuilder->setDefinition('subscriber_service_id', $subscriber);

Expand Down
4 changes: 2 additions & 2 deletions components/form.rst
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ option when building each field:
->add('dueDate', DateType::class, array(
'constraints' => array(
new NotBlank(),
new Type('\DateTime'),
new Type(\DateTime::class),
)
))
->getForm();
Expand All @@ -679,7 +679,7 @@ option when building each field:
->add('dueDate', DateType::class, array(
'constraints' => array(
new NotBlank(),
new Type('\DateTime'),
new Type(\DateTime::class),
)
))
->getForm();
Expand Down
8 changes: 4 additions & 4 deletions components/security/authentication.rst
Original file line number Diff line number Diff line change
Expand Up @@ -172,19 +172,19 @@ user. This allows you to use different encoding strategies for different
types of users. The default :class:`Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactory`
receives an array of encoders::

use Acme\Entity\LegacyUser;
use Symfony\Component\Security\Core\Encoder\EncoderFactory;
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
use Symfony\Component\Security\Core\User\User;

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

$encoders = array(
'Symfony\\Component\\Security\\Core\\User\\User' => $defaultEncoder,
'Acme\\Entity\\LegacyUser' => $weakEncoder,

User::class => $defaultEncoder,
LegacyUser::class => $weakEncoder,
// ...
);

$encoderFactory = new EncoderFactory($encoders);

Each encoder should implement :class:`Symfony\\Component\\Security\\Core\\Encoder\\PasswordEncoderInterface`
Expand Down
6 changes: 4 additions & 2 deletions components/security/authorization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,11 @@ on a "remember-me" cookie, or even authenticated anonymously?
.. code-block:: php

use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolver;
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;

$anonymousClass = 'Symfony\Component\Security\Core\Authentication\Token\AnonymousToken';
$rememberMeClass = 'Symfony\Component\Security\Core\Authentication\Token\RememberMeToken';
$anonymousClass = AnonymousToken::class;
$rememberMeClass = RememberMeToken::Class;

$trustResolver = new AuthenticationTrustResolver($anonymousClass, $rememberMeClass);

Expand Down
9 changes: 6 additions & 3 deletions components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ Deserializing an Object
You'll now learn how to do the exact opposite. This time, the information
of the ``Person`` class would be encoded in XML format::

use Acme\Person;

$data = <<<EOF
<person>
<name>foo</name>
Expand All @@ -141,7 +143,7 @@ of the ``Person`` class would be encoded in XML format::
</person>
EOF;

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

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

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

$person = new Acme\Person();
// ...
$person = new Person();
$person->setName('bar');
$person->setAge(99);
$person->setSportsman(true);
Expand All @@ -167,7 +170,7 @@ The serializer can also be used to update an existing object::
</person>
EOF;

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

This is a common need when working with an ORM.
Expand Down
14 changes: 6 additions & 8 deletions console/commands_as_services.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ with ``console.command``:
.. code-block:: php

// app/config/config.php
use AppBundle\Command\MyCommand;

$container
->register(
'app.command.my_command',
'AppBundle\Command\MyCommand'
)
->register('app.command.my_command', MyCommand::class)
->addTag('console.command')
;

Expand Down Expand Up @@ -164,13 +163,12 @@ inject the ``command.default_name`` parameter:
.. code-block:: php

// app/config/config.php
use AppBundle\Command\MyCommand;

$container->setParameter('command.default_name', 'Javier');

$container
->register(
'app.command.my_command',
'AppBundle\Command\MyCommand',
)
->register('app.command.my_command', MyCommand::class)
->setArguments(array('%command.default_name%'))
->addTag('console.command')
;
Expand Down
6 changes: 4 additions & 2 deletions console/logging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,12 @@ First configure a listener for console exception events in the service container
.. code-block:: php

// app/config/services.php
use AppBundle\EventListener\ConsoleExceptionListener;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

$definitionConsoleExceptionListener = new Definition(
'AppBundle\EventListener\ConsoleExceptionListener',
ConsoleExceptionListener::class,
array(new Reference('logger'))
);
$definitionConsoleExceptionListener->addTag(
Expand Down Expand Up @@ -206,11 +207,12 @@ First configure a listener for console terminate events in the service container
.. code-block:: php

// app/config/services.php
use AppBundle\EventListener\ErrorLoggerListener;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

$definitionErrorLoggerListener = new Definition(
'AppBundle\EventListener\ErrorLoggerListener',
ErrorLoggerListener::class,
array(new Reference('logger'))
);
$definitionErrorLoggerListener->addTag(
Expand Down
3 changes: 2 additions & 1 deletion controller/error_pages.rst
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,11 @@ In that case, you might want to override one or both of the ``showAction()`` and
.. code-block:: php

// app/config/services.php
use AppBundle\Controller\CustomExceptionController;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Definition;

$definition = new Definition('AppBundle\Controller\CustomExceptionController', array(
$definition = new Definition(CustomExceptionController::class, array(
new Reference('twig'),
'%kernel.debug%'
));
Expand Down
Loading