Edit report at http://bugs.php.net/bug.php?id=54171&edit=1
ID: 54171
Comment by: hellosexyprout at gmail dot com
Reported by: hellosexyprout at gmail dot com
Summary: is_callable() returns false on callable functions
when not specifying namespace
Status: Bogus
Type: Bug
Package: Scripting Engine problem
Operating System: Ubuntu 10.10
PHP Version: 5.3.5
Block user comment: N
Private report: N
New Comment:
Great, I'm okay with that.
But how do explain this?
<?php
namespace Foo;
function bar() {}
function hello_world() {}
bar(); // works
hello_word(); // fails
Previous Comments:
------------------------------------------------------------------------
[2011-03-05 22:49:47] [email protected]
It is not incoherent, see...
"How does an unqualified function name or unqualified constant name like
name resolve?
Function or constant names that do not contain a backslash like name can
be resolved in 2 different ways.
First, the current namespace name is prepended to name.
Finally, if the constant or function name does not exist in the current
namespace, a global constant or function name is used if it exists."
-- http://docs.php.net/manual/en/language.namespaces.faq.php
namespace foo;
function bar() { }
bar(); // foo\ is prepended in compile-time
$x = 'bar'; // obviously foo\ will not be prepended
$x(); // hence 'bar' is not found
------------------------------------------------------------------------
[2011-03-05 22:33:26] hellosexyprout at gmail dot com
But then PHP's behavior is incoherent because:
<?php
namespace NS;
class HelloWorld {}
function hello_world() {}
// This will work
new HelloWorld;
// This will fail
hello_word();
?>
And anyway, being able to call bar() while 'bar' isn't
considered callable by is_callable() (see report) is totally
incoherent. Or I'm missing something big.
------------------------------------------------------------------------
[2011-03-05 22:05:28] [email protected]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php
This is expected, see #51651
------------------------------------------------------------------------
[2011-03-05 21:04:27] hellosexyprout at gmail dot com
PLEASE NOTE: I inverted the actual and expected results, sorry.
------------------------------------------------------------------------
[2011-03-05 21:00:08] hellosexyprout at gmail dot com
Description:
------------
First sorry, I can only test this case with PHP 5.3.3 but I
didn't find any bugs referring to is_callable() since then, so I
think it's worth reporting.
The problem is that is_callable() will return false if you don't
specify the eventual namespace you're using.
Test script:
---------------
<?php
namespace Foo;
function bar() {
return 'bar!';
}
var_dump(is_callable('bar'));
var_dump(is_callable('\Foo\bar'));
var_dump(bar());
var_dump(\Foo\bar());
Expected result:
----------------
bool(false)
bool(true)
string(4) "bar!"
string(4) "bar!"
Actual result:
--------------
bool(true)
bool(true)
string(4) "bar!"
string(4) "bar!"
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=54171&edit=1