Join GitHub today
Importing and aliases vs. fully qualified namespaces #872
I think that Magento2 should import namespaces and use aliases instead of using fully qualified namespaces all over the place.
Here is an example.
What I suggest is that instead of:
We could have:
Here are the benefits from this:
Let me know what you think.
I totally second the importing approach
I would rather forbid aliases usage or allow them only for exceptional cases. Proper class name must contain all data necessary to understand the purpose of class. There are a lot of names with directory-per-word approach and would be better to rename classes than introduce aliases each time you want to use them, for instance:
You may notice that there are a lot of code in Magento2 using short names already. But in code which was not changed for a long time there are still a lot of fully-qualified class names. Would be nice to replace fully-qualified class names with imports all over the code and enforce short name usage with static test (for example, check that
What tool would you suggest for such transformation? The closest ready-to-use utility I found so far is PHP Refactoring Browser, but "optimize use statements" feature here does not work as expected: type-hinted class names in constructor/other methods, PHPDoc blocks are not updated.
So, except the IDE abilities which I didn't analyze, I would build such script using one of:
Thanks @orlangur for the comment.
I just picked that Exception class for no reason, and yes I should have made a better alias, but for me even importing without alias is still better than fully qualified name. Maybe aliases could be used only if we have two classes with the same name.
To tell you the truth, I am not sure which tool would be the best to do this automatically. I know some IDEs can do that, but I am not sure how reliable are they. I guess it would be hard to find a reliable tool.
This was referenced
Jan 8, 2015
Thank you for your submission.
We recently made some changes to the way we process GitHub submissions to more quickly identify and respond to core code issues.
We are closing this GitHub ticket and have moved your request to the new forum.