I do like Acme::class syntax, when you start using it you cannot stop.
Guys from C# implemented similar but more powerful feature: nameof(Acme),
nameof(Acme.AnyMethod)
https://msdn.microsoft.com/en-us/magazine/dn802602.aspx
Imagine how much confidence it will give you while renaming your methods,
class names etc.
Hi!
On Tue, May 12, 2015 at 9:29 AM, Alexandr Marchenko <
marchenko.alexandr@gmail.com> wrote:
Guys from C# implemented similar but more powerful feature: nameof(Acme),
nameof(Acme.AnyMethod)https://msdn.microsoft.com/en-us/magazine/dn802602.aspx
Imagine how much confidence it will give you while renaming your methods,
class names etc.
Neat. So, how would that work in this scenario?
class God {
public static function create($method) {
call_user_func([ 'God', $method ]);
}
private static function chicken() {
echo 'Let there be chicken!';
}
}
God::create('chicken');
I understand nameof(God) would replace the literal string 'God', but how
would one replace the literal 'chicken' with the appropriate compile time
reference? Perhaps: nameof(God::chicken)
Also, since nameof() is a compile-time conversion
http://tryroslyn.azurewebsites.net/#b:master/K4Zwlgdg5gBAygTxAFwKYFsDcAoADsAIwBswBjGUogQxBBgDEB7R7Ab2xk5n2LJgDdGYACYwAQlQBOAChSTIsXFKroAjAEoOXdl10wAwowghGRVADoA6vLQAZSKmkQVqRgDNpTRuvU49XQ2NTC2swOwcnF3dpCUkfP38DIxMzKxtUewhHZ3RXDyVJFQ1fLU4AX2wyoAA,
nameof() wouldn't help a value construct like this:
God::create($_REQUEST['animal']);
So, overall, how does nameof(God) aid in refactoring more than a find and
subsequent replace, like ag https://github.com/ggreer/the_silver_searcher
--php "'God'"?
bishop
Hello Bishop,
For your first example I do not know what will be the best way but assume something like:
God::create(God::chicken);
In cases when you are giving strings to PHP (like in example with $_REQUEST[‘animal’]) all should work as is
Idea of nameof (at least in my understanding) is to reduce string usage and replace them with “strongly” typed “pointers” which can give us feedback while script is processed (like when you misstyped method name php will give you fatal error).
You are right – tools like ag https://github.com/ggreer/the_silver_searcher --php "'God'" will do the work, but when it comes to rename method – there might be troubles, for example if I have code like:
class God { public function chicken() {} }
$data = $_REQUEST[‘chicken’];
calluser_func(God::class, ‘chicken’);
you cannot just rename word “chicken” in your code.
Also while you are typing God::chi| - you can get autocomplete, which will help you write less code and make less mistakes
Idea of nameof (at least in my understanding) is to reduce string usage
and replace them with “strongly” typed “pointers” which can give us
feedback while script is processed (like when you misstyped method name
php will give you fatal error).
The validation part makes sense, but from the description linked, there are no pointers involved, it just results in an ordinary string. In many cases, the string is only needed in the first place because there isn't a way of getting a better pointer - e.g. when passing in a class name to a factory, or a method name to some dynamic call mechanism. It would seem like a better idea to tackle that underlying problem, with some mechanism for passing classes, methods, etc as first-class types.
The example in that blog post of accessing a parameter name seems very ugly to me. If a function takes positional, not named, arguments, then why should any outside code care what name they're implemented under? There is no wrong answer to "what name should I mention in this error message?".
With named parameters, that all changes, because names become part of the signature, not the implementation. My current feeling is that parameters should be named independent of the local variables they assign to, e.g.
function foo($bar as x, $baz as y) {
return $bar / $baz;
}
foo(y: 42, x: 420); // 10
In which case, you'd sometimes want some equivalent of nameof($bar) to give you 'x', which would be very messy.
Regards,
Rowan Collins
[IMSoP]