Hi,
It has been brought to my attention that my consistent use of \ prefixing
of global functions is an eyesore, but I've got a simple little PoC that
shows why I do it, and now I'm wondering if the behavior I'm working around
should qualify as a PHP bug?
Scott Arciszewski
Chief Development Officer
Paragon Initiative Enterprises <https://paragonie.com
Scott,
Hi,
It has been brought to my attention that my consistent use of \ prefixing
of global functions is an eyesore, but I've got a simple little PoC that
shows why I do it, and now I'm wondering if the behavior I'm working around
should qualify as a PHP bug?Scott Arciszewski
Chief Development Officer
Paragon Initiative Enterprises https://paragonie.com
It's not a bug. It's explicit. The \
prefix says it's a global
function. The non-prefixed looks in the current namespace and then
falls back to global.
Also, in PHP 5.6+ you can do something slightly different to achieve
the same result: use function.
namespace Foo {
use function random_int;
var_dump(random_int(1, 100));
var_dump(\random_int(1, 100));
}
That will always behave the same no matter if someone defines a
namespace-specific random_int function, it will always use the global
version.
This actually opens a pretty interesting edge-case, where you can
define a function in the namespace and use the global one without
needing a "".
namespace Foo {
use function strlen;
function strlen(string $str) : int {
return strlen($str) + 1;
}
}
That actually is not recursive. This is a bit non-obvious, but
completely follows the rules set forth. So I don't think there's a bug
(though we may want to raise a notice in that collision case)...
Anthony
Hi!
It has been brought to my attention that my consistent use of \ prefixing
of global functions is an eyesore, but I've got a simple little PoC that
shows why I do it, and now I'm wondering if the behavior I'm working around
should qualify as a PHP bug?
No, it's not a bug, it is a feature. You can override function in a
namespace. It's still not a reason to mark every function you are not
overriding with . Unless, of course, you are importing foreign (as, not
controlled by you) code into your namespace, in which case I think
that's where the problem lies.
Stas Malyshev
smalyshev@gmail.com
Stanislav, Anthony:
Okay, I just wanted to make sure I wasn't overlooking an unintended
behavior. I'm going to continue using \explicit_prefixing().
http://userjac.com/wp-content/uploads/2014/05/haters-gonna-hate-cool-dog.jpg
Scott Arciszewski
Chief Development Officer
Paragon Initiative Enterprises https://paragonie.com
On Fri, Dec 4, 2015 at 2:24 PM, Stanislav Malyshev smalyshev@gmail.com
wrote:
Hi!
It has been brought to my attention that my consistent use of \ prefixing
of global functions is an eyesore, but I've got a simple little PoC that
shows why I do it, and now I'm wondering if the behavior I'm working
around
should qualify as a PHP bug?No, it's not a bug, it is a feature. You can override function in a
namespace. It's still not a reason to mark every function you are not
overriding with . Unless, of course, you are importing foreign (as, not
controlled by you) code into your namespace, in which case I think
that's where the problem lies.Stas Malyshev
smalyshev@gmail.com