Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Why use ::class only for fully qualified class names? #777

Closed
Vinai opened this issue Aug 1, 2016 · 5 comments
Closed

Why use ::class only for fully qualified class names? #777

Vinai opened this issue Aug 1, 2016 · 5 comments
Assignees

Comments

@Vinai
Copy link
Contributor

Vinai commented Aug 1, 2016

The page http://devdocs.magento.com/guides/v2.0/coding-standards/code-standard-php.html states

use PHP 5.5’s ::class keyword when referring to the fully qualified class name outside of that class

Since I'm not sure if that is on purpose or by accident, I didn't open a PR, but I think it makes no sense to only use the ::class constant for full class names and use strings for the rest.

Since the::class always is mapped to a fully qualified class name by the PHP preprocessor, there should be no need to use it only for fully qualified class names. It should be used every time a class name is referenced as a string.

Examples:

Non imported class name:

// Resolves to "\Magento\Path\To\Class"
$this->get(\Magento\Path\To\Class::class);

Imported class name:

use Foo\Bar;

// ... other code

// Resolves to "\Foo\Bar"
$this->get(Bar::class);

Namespace relative class name:

namespace Baz;

// ... other code

// Resolves to "\Baz\Qux"
$this->get(Qux::class);

Import relative class name

use Magento\Framework\Data;

// ... other code

// Resolves to "\Magento\Framework\Data\Helper"
$this->get(Data\Helper::class);
@ghost
Copy link

ghost commented Aug 1, 2016

@antonkril Can you comment? Thanks

@ghost
Copy link

ghost commented Aug 1, 2016

@Vinai It looks like this will be implemented soon. Does that help?

@Vinai
Copy link
Contributor Author

Vinai commented Aug 2, 2016

Well... it's related, but I opened this issue so the wording in the documentation can be clarified. So the implementation you linked is basically the core team following the recommendation documented here.

@ghost ghost self-assigned this Aug 2, 2016
@jcalcaben jcalcaben self-assigned this Aug 2, 2016
@ghost ghost removed their assignment Aug 3, 2016
@antonkril
Copy link
Contributor

Don't see reasons to use strings for non-full names. Wording should be fixed

@jcalcaben
Copy link
Contributor

I agree and see how the wording can be confusing. @Vinai may I use your examples in our docs for clarification?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants