-
Notifications
You must be signed in to change notification settings - Fork 7.9k
add nameof operator #11172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
add nameof operator #11172
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9443,6 +9443,35 @@ static void zend_compile_include_or_eval(znode *result, zend_ast *ast) /* {{{ */ | |
} | ||
/* }}} */ | ||
|
||
static void zend_compile_nameof(znode *result, zend_ast *ast) /* {{{ */ | ||
{ | ||
zend_ast *var_ast = ast->child[0]; | ||
zend_ast *name_ast; | ||
|
||
// todo: proper error handling and better error messages | ||
|
||
switch (var_ast->kind) { | ||
case ZEND_AST_VAR: | ||
case ZEND_AST_CALL: | ||
name_ast = var_ast->child[0]; | ||
ZVAL_COPY(&result->u.constant, zend_ast_get_zval(name_ast)); | ||
result->op_type = IS_CONST; | ||
break; | ||
case ZEND_AST_PROP: | ||
case ZEND_AST_NULLSAFE_PROP: | ||
case ZEND_AST_STATIC_PROP: | ||
case ZEND_AST_CLASS_CONST: | ||
name_ast = var_ast->child[1]; | ||
ZVAL_COPY(&result->u.constant, zend_ast_get_zval(name_ast)); | ||
result->op_type = IS_CONST; | ||
break; | ||
default: | ||
zend_error_noreturn(E_COMPILE_ERROR, | ||
"Cannot use nameof() on the result of an expression or something with an ambiguous name"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can "something" be more specific in this error, or is it as specific as it can be? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes and yes; that's actually a good question. I imagine that given more examples, more helpful error messages can be created, but there are "somethings" that are ambiguous or don't make sense:
In the grammar, I chose an expression as what can go in the brackets/parenthesis, but it doesn't make sense to accept literally any expression. Perhaps there is something better to use (or construct), but I didn't want to spend too much time on it in case there was a name for these more ambiguous things that I don't have the imagination for. |
||
} | ||
} | ||
/* }}} */ | ||
|
||
static void zend_compile_isset_or_empty(znode *result, zend_ast *ast) /* {{{ */ | ||
{ | ||
zend_ast *var_ast = ast->child[0]; | ||
|
@@ -10423,6 +10452,9 @@ static void zend_compile_expr_inner(znode *result, zend_ast *ast) /* {{{ */ | |
case ZEND_AST_EMPTY: | ||
zend_compile_isset_or_empty(result, ast); | ||
return; | ||
case ZEND_AST_NAMEOF: | ||
zend_compile_nameof(result, ast); | ||
return; | ||
case ZEND_AST_SILENCE: | ||
zend_compile_silence(result, ast); | ||
return; | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
a[a[]]=1&a[b[]]=3 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--TEST-- | ||
nameof operator | ||
--FILE-- | ||
<?php | ||
$a = 'test'; | ||
echo 'name: ' . nameof($a); | ||
?> | ||
--EXPECT-- | ||
name: a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are your ideas for proper error handling? Happy to thinker with you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MelvinRook, I'm actually still trying to come up with something in the RFC that makes sense (and will probably come up in discussions). For example, should this be an error and what might the error message be:
In this specific implementation, no error/warning message is shown if you use a non-existent variable/method/member/etc, nor is any autoloading triggered. This might be desired for performance/optimization reasons, but the entire point is to make refactoring of messages/strings easier, thus inconsistencies could be missed.
If a message is emitted, what should be emitted? Notices, warnings, errors, exceptions? I personally, would favor a normal message, so the above would emit a warning ("Undefined variable $x in ...") but I can see how other people might want something special to happen.
Lots to think about and I haven't particularly settled on any particular implementation. I'd love to hear any arguments for any of them.