php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54171 is_callable() returns false on callable functions when not specifying namespace
Submitted: 2011-03-05 21:00 UTC Modified: 2011-03-05 23:08 UTC
From: hellosexyprout at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.5 OS: Ubuntu 10.10
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: hellosexyprout at gmail dot com
New email:
PHP Version: OS:

 

 [2011-03-05 21:00 UTC] 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!"


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-03-05 21:04 UTC] hellosexyprout at gmail dot com
PLEASE NOTE: I inverted the actual and expected results, sorry.
 [2011-03-05 22:05 UTC] felipe@php.net
-Status: Open +Status: Bogus
 [2011-03-05 22:05 UTC] felipe@php.net
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 22:33 UTC] 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:49 UTC] felipe@php.net
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:51 UTC] felipe@php.net
-Package: *General Issues +Package: Scripting Engine problem
 [2011-03-05 23:06 UTC] hellosexyprout at gmail dot com
Great, I'm okay with that.

But how do explain this?

<?php
namespace Foo;

function bar() {}
function hello_world() {}

bar(); // works
hello_word(); // fails
 [2011-03-05 23:08 UTC] felipe@php.net
You mistyped 'world' on function call :-)
 [2011-03-05 23:16 UTC] hellosexyprout at gmail dot com
Damn it, thanks!
Well, sorry to have bothered you and thank you for answering my 
questions. ;)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue May 06 19:01:28 2025 UTC