1

When I am looking for an information about any class in api documentation, I often encounter the situation when there are several classes with the same name and the same description, but different location.

For example, there are two classes with name YamlDiscovery and the same description.

How should one select a needed class for oneself? Can use any class? If so, what for they were added both into Drupal 8?

closed as off-topic by kiamlaluno Jan 1 '17 at 17:56

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on programming, PHP, SQL, etc. that do not relate directly to Drupal are off-topic here, but can be asked on Stack Overflow." – kiamlaluno
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    This is a plain PHP question about using classes with the same name but different namespaces. Drupal doesn't alter the PHP syntax, so you do as you would with plain PHP: You qualify the class name with the namespace, or you use an alias in the use directive as said in the given answer. Keep in mind that our task is not teaching PHP, but Drupal; everything about PHP syntax is off-topic for us. – kiamlaluno Jan 1 '17 at 17:56
3

That's a bad example, it is then used as another name to avoid a name conflict use Drupal\Component\Discovery\YamlDiscovery as ComponentYamlDiscovery;.

Usually you can see the purpose of the class when you look at the namespace or the corresponding file location. It's OK to use the same class name in different namespaces. As you can see in this issue there can be different opinions how descriptive class names should be and how much you should rely on the namespace of the class to make the purpose clear:

https://www.drupal.org/node/2408667

1

So we have 2 classes, one belongs in the Drupal\Component\Discovery namespace and the other in Drupal\Core\Discovery.

The Component namespace has code that is independent and the idea is that you should be able to use these classes outside of Drupal, so classes in components MUST NOT depend on any Drupal specific code, but MAY have dependencies on other Drupal components or external libraries.

Classes in Core namespace however are allowed to have Drupal specific code and if we look at the YamlDiscovery class in the Core-namespace we can see it extends the one from the Component.

namespace Drupal\Core\Discovery;

use Drupal\Component\Discovery\YamlDiscovery as ComponentYamlDiscovery;
use Drupal\Core\Serialization\Yaml;

/**
 * Provides discovery for YAML files within a given set of directories.
 *
 * This overrides the Component file decoding with the Core YAML implementation.
 */
class YamlDiscovery extends ComponentYamlDiscovery {

So, regarding which one you should use, I did some digging and the YamlDiscovery class in Core does use the PECL Yaml extension if it is available which is a performance boost. The issue is here: https://www.drupal.org/node/1920902

Generally I would prefer to use Drupal components as they are not tightly coupled to Drupal and loose coupling is considered good practises. However, in many cases Drupal Core adds things that you may need or enhances things like performance in this case. So if you have the PECL Yaml extension, go ahead and use the one from Core. Here is the link for the change record:

To give you another example, here is a good explanation on when the Drupal component and Drupal core differs much more. https://www.drupal.org/node/1834108 In this case I would use the latter (the class from Core) since it adds translation and formatted date values features.

Not the answer you're looking for? Browse other questions tagged or ask your own question.