Skip to content

Commit 68e79d2

Browse files
committed
Introduce with as an Alias for Namespace use Keyword
1 parent dfff6ac commit 68e79d2

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

RFC-with-alias-xiaoma.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Proposed by: [xiaoma]
2+
# Date: 2025-5-23
3+
4+
# PHP RFC: Introduce `with` as an Alias for Namespace `use` Keyword
5+
6+
## Proposal Background
7+
8+
Currently in PHP, the `use` keyword serves two distinct purposes:
9+
1. **Namespace Importing**: Used to import/alias namespaces, classes, functions, and constants
10+
```php
11+
use Foo\Bar as Baz;
12+
use function Foo\hello as greet;
13+
```
14+
Closure Variable Capture: Used in anonymous functions to inherit variables from parent scope
15+
16+
Problem Analysis
17+
18+
Semantic Confusion: The namespace use actually performs "import/reference" operations, but its literal meaning suggests "usage", which may mislead beginners into thinking it performs file operations or immediate usage
19+
Learning Curve: Beginners often struggle with the dual meaning of use, requiring additional context explanation
20+
Language Consistency: Modern languages (Dart/Kotlin etc.) use more explicit terms like with or import
21+
22+
Proposal
23+
24+
Introduce with as a full alias for namespace-related use:
25+
26+
```php
27+
with Foo\Bar as Baz; // Equivalent to use Foo\Bar as Baz;
28+
with function Foo\hello; // Equivalent to use function Foo\hello;
29+
```
30+
Benefits
31+
Clearer Semantics: with better conveys "reference/borrow" meaning, avoiding file operation misconceptions
32+
"with this namespace..." is more accurate than "use this namespace..."
33+
34+
Zero-Cost Migration:
35+
36+
Fully backward compatible, no changes required for existing code
37+
Developers can freely choose the more semantic keyword
38+
39+
Beginner Friendly:
40+
41+
Reduces cognitive load for newcomers, aligning with PHP's "easiest to learn" philosophy
42+
Language Evolution: Maintains PHP's modern image while respecting historical design
43+
44+
Backward Compatibility
45+
46+
No BC breaks: Pure syntactic sugar, no behavior changes
47+
48+
No performance impact: Simple lexer mapping, no runtime overhead
49+
50+
Toolchain compatibility: All static analysis tools work without modification
51+
Voting Suggestion
52+
53+
Propose as a "Language Improvement" RFC requiring 2/3 majority.
54+
55+
Example Comparison
56+
```php
57+
// Traditional syntax
58+
use Vendor\Package\ClassName;
59+
use function Vendor\Package\functionName;
60+
use const Vendor\Package\CONSTANT;
61+
62+
// New syntax
63+
with Vendor\Package\ClassName;
64+
with function Vendor\Package\functionName;
65+
with const Vendor\Package\CONSTANT;
66+
```
67+
FAQ
68+
Q: Will use syntax be deprecated?
69+
70+
A: No, this is an optional alias mechanism. use will be permanently supported.
71+
72+
Q: Will closure use also get an alias?
73+
A: Not in scope of this proposal, keeping closure syntax stable.
74+
75+
76+
I know this may be an insignificant improvement, but I think it's necessary

0 commit comments

Comments
 (0)