Skip to content

Instantly share code, notes, and snippets.

@Danack
Last active August 29, 2015 14:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Danack/5e4870fed5eedf3a3b61 to your computer and use it in GitHub Desktop.
Save Danack/5e4870fed5eedf3a3b61 to your computer and use it in GitHub Desktop.
GroupedUse

Introduction

This RFC hopes to improves PHP namespace implementation by introducing the concept of Grouped Use Declarations:

Proposal

Grouped use declarations, allow people to group use statements by namespace. Using common PHP library examples, the following use declarations are equivalents:

Justification (Put this before the syntax choice)

  • Grouping use statements makes it easier to read that multiple entries are from the same module. For the first example in this RFC, instead of having to read 'FooLibrary\Bar\Baz\Biz' four times to see that the namespace is the same for each entry, it is clear by the grouping syntax that the entries all come from the same namespace.

  • Grouping use statements makes it easier to see in pull requests that a dependency on a new module has been introduced. For example a Pull Request where a new dependency on 'Consolidation' has been introduced:

use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\ChoiceQuestion as Choice;
+use Symfony\Component\Console\Question\OptionQuestion;
+use Symfony\Component\Consolidation\Question\OptionQuestion;
use Symfony\Component\Console\Question\Question;

With the current syntax you need to read to the 30th character of the 2nd modified line to realise that the PR adds a new dependency. With the 'batch use' syntax, because the new dependency is not in the same namespace as the existing 'grouped use' statements it can't be slipped into that block.

use Symfony\Component\Console\Question {
	ConfirmationQuestion,
	ChoiceQuestion as Choice;
+	OptionQuestion,
	Question,
};

+use Symfony\Component\Consolidation\Question\OptionQuestion;

About The Syntax Choice

The syntax chosen is inline with the current trait adaptation syntax to make it look and feel similar to existing PHP standards.

(Delete the trait example, it adds nothing except confusion - I thought it was for the group use stuff).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment