rfc:batch_use_declarations
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:batch_use_declarations [2015/01/29 16:31] – fix some code samples that had duplicated use statements marcio | rfc:batch_use_declarations [2017/09/22 13:28] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Batch Use Declarations ====== | + | Moved to https:// |
- | * Date: 2015-01-28 | + | |
- | * Author: Márcio Almada, marcio.web2@gmail.com | + | |
- | * Status: Draft | + | |
- | * First Published at: http:// | + | |
- | * Patch: https:// | + | |
- | + | ||
- | ===== Introduction ===== | + | |
- | This RFC represents an attempt to improve current PHP namespace implementation by introducing the concept of **Batch Use Declarations**: | + | |
- | + | ||
- | <code php> | + | |
- | // Proposed batch use syntax: | + | |
- | + | ||
- | use FooLibrary\Bar\Baz\Biz { ClassA, ClassB, ClassC as Fizbo, ClassD }; | + | |
- | + | ||
- | // Compared to current use syntax: | + | |
- | + | ||
- | use FooLibrary\Bar\Baz\Biz\ClassA; | + | |
- | use FooLibrary\Bar\Baz\Biz\ClassB; | + | |
- | use FooLibrary\Bar\Baz\Biz\ClassC as Fizbo; | + | |
- | use FooLibrary\Bar\Baz\Biz\ClassD; | + | |
- | </ | + | |
- | + | ||
- | ===== Proposal ===== | + | |
- | Batch use declarations, | + | |
- | + | ||
- | <code php> | + | |
- | // Current use syntax: | + | |
- | + | ||
- | use Doctrine\Common\Collections\Expr\Comparison; | + | |
- | use Doctrine\Common\Collections\Expr\Value; | + | |
- | use Doctrine\Common\Collections\Expr\CompositeExpression; | + | |
- | + | ||
- | // Proposed batch use syntax: | + | |
- | + | ||
- | use Doctrine\Common\Collections\Expr { Comparison, Value, CompositeExpression }; | + | |
- | </ | + | |
- | + | ||
- | === Compound Namespaces === | + | |
- | Compound namespaces are also allowed. For instance, the following use declarations are equivalents: | + | |
- | + | ||
- | <code php> | + | |
- | // Current use syntax: | + | |
- | + | ||
- | use Symfony\Component\Console\Helper\Table; | + | |
- | use Symfony\Component\Console\Input\ArrayInput; | + | |
- | use Symfony\Component\Console\Output\NullOutput; | + | |
- | use Symfony\Component\Console\Question\Question; | + | |
- | use Symfony\Component\Console\Input\InputInterface; | + | |
- | use Symfony\Component\Console\Output\OutputInterface; | + | |
- | use Symfony\Component\Console\Question\ChoiceQuestion as Choice; | + | |
- | use Symfony\Component\Console\Question\ConfirmationQuestion; | + | |
- | + | ||
- | // Proposed batch use syntax: | + | |
- | + | ||
- | use Symfony\Component\Console { | + | |
- | Helper\Table, | + | |
- | Input\ArrayInput, | + | |
- | Input\InputInterface, | + | |
- | Output\NullOutput, | + | |
- | Output\OutputInterface, | + | |
- | Question\Question, | + | |
- | Question\ChoiceQuestion as Choice, | + | |
- | Question\ConfirmationQuestion, | + | |
- | }; | + | |
- | </ | + | |
- | + | ||
- | This is also a real use case. Check out the Laravel source code: | + | |
- | + | ||
- | * [[https:// | + | |
- | * [[https:// | + | |
- | + | ||
- | === Non Mixed Use Declarations === | + | |
- | As expected, non mixed batch use declarations are supported: | + | |
- | + | ||
- | <code php> | + | |
- | // Current use syntax: | + | |
- | + | ||
- | use function foo\math\sin, | + | |
- | use const foo\math\PI, | + | |
- | + | ||
- | // Proposed non mixed batch use syntax: | + | |
- | + | ||
- | use function foo\math { sin, cos, cosh }; | + | |
- | use const foo\math { PI, E, GAMMA, GOLDEN_RATIO }; | + | |
- | </ | + | |
- | + | ||
- | === Mixed Use Declarations === | + | |
- | Current namespace implementation does not allow mixed imports of functions, constants and classes. With the approval of this RFC, for completeness, | + | |
- | + | ||
- | <code php> | + | |
- | // Current use syntax: | + | |
- | + | ||
- | use foo\math\Math; | + | |
- | use const foo\math\PI; | + | |
- | use function foo\math\sin, | + | |
- | + | ||
- | // Proposed mixed batch use syntax: | + | |
- | + | ||
- | use foo\math { Math, const PI, function sin, function cos, function cosh }; | + | |
- | + | ||
- | </ | + | |
- | + | ||
- | ===== About The Syntax Choice ===== | + | |
- | The current choice for batch use syntax is basically a small variation from the current trait adaptation syntax: | + | |
- | + | ||
- | <code php> | + | |
- | use A, B { | + | |
- | B:: | + | |
- | A::bigTalk insteadof B; | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | Such design choice allows the feature to stay familiar and intuitive to most PHP user base. | + | |
- | + | ||
- | ===== Optional: Nested Batch Use Declarations ===== | + | |
- | **Nesting** would allow the following use case: | + | |
- | + | ||
- | <code php> | + | |
- | use Symfony\Component\Console { | + | |
- | Helper\Table, | + | |
- | Input { ArrayInput, InputInterface } | + | |
- | Output { NullOutput, OutputInterface }, | + | |
- | Question { Question, ChoiceQuestion, | + | |
- | }; | + | |
- | </ | + | |
- | + | ||
- | As the author of this RFC, I consider the **nesting** option **too complex** to be introduced. But since many people mentioned this **possibility**, | + | |
- | + | ||
- | ===== Backward Incompatible Changes ===== | + | |
- | There is no BC breaks with current implementation or feature concepts. | + | |
- | + | ||
- | ===== Proposed PHP Version(s) ===== | + | |
- | This is proposed for the next PHP x, which at the time of this writing would be PHP 7. | + | |
- | + | ||
- | ===== RFC Impact ===== | + | |
- | + | ||
- | === On Backward Compatibility === | + | |
- | This RFC is backwards compatible with previous PHP releases. | + | |
- | + | ||
- | ===== Open Issues ===== | + | |
- | * Should nested batch use declarations be allowed? | + | |
- | + | ||
- | ===== Unaffected PHP Functionality ===== | + | |
- | Original namespace implementation is not affected by the addition of batch use declarations. Current syntax would still be valid. | + | |
- | + | ||
- | ===== Proposed Voting Choices ===== | + | |
- | As this RFC represents a language change a two third majority is required. | + | |
- | + | ||
- | ===== Patches and Tests ===== | + | |
- | The implementation aims to be minimal and of easy maintenance. I created a PR so that the patch diff can be viewed easily: https:// | + | |
- | + | ||
- | The current initial implementation can be found on my PHP [[https:// | + | |
- | + | ||
- | ===== References ===== | + | |
- | There is no found record related to batch use declarations on mailing lists. | + | |
- | + | ||
- | ==== Support in other languages ==== | + | |
- | Other languages have similar ways to import multiple entities from a given package or module: | + | |
- | + | ||
- | * [[http:// | + | |
- | * [[https:// | + | |
- | + | ||
- | ===== Rejected Features ===== | + | |
- | Awaiting discussion. | + |
rfc/batch_use_declarations.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1