Bug #68541 [NEW]: namespace fallback for functions unexpected when defined after usage in class.
| From: | markus at malkusch dot de | Date: | Wed, 03 Dec 2014 12:17:20 +0000 |
| Subject: | Bug #68541 [NEW]: namespace fallback for functions unexpected when defined after usage in class. | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-188895@lists.php.net to get a copy of this message | ||
From: markus at malkusch dot de
Operating system: Linux
PHP version: 5.5.19
Package: Class/Object related
Bug Type: Bug
Bug description:namespace fallback for functions unexpected when defined after usage in class.
Description:
------------
PHP's namespace fallback policy for functions is a great feature for
mocking built-in functions like time() for unit tests. I'm using this in
my mocking library php-mock: https://github.com/malkusch/php-mock
I experienced a suprising behaviour when an unqualified call in a class
happened before the definition of the namespaced function. E.g. when
calling in foo\Foo time() and defining afterwards foo\time(). I would
expect that after that definition time() in foo\Foo would resolve to
foo\time(). Instead it resolves to whatever was resolvable during the
first call.
In the example you see two cases. In the rand() case I call
foo\Foo::rand() first after the definition of foo\rand(). Those
assertions are fine and rand() resolves to foo\rand().
In the time case() I call foo\Foo::time() first before the definition of
foo\time(). The call after defining foo\time() still resolves to
\time(). I would expect here a resolution to foo\time(). If this is
intentional the documentation might be more clear about this.
Test script:
---------------
<?php
namespace foo;
class Foo
{
public static function time()
{
return time();
}
public static function rand()
{
return rand();
}
}
Foo::time(); // If you remove this line all assertions are true.
// It doesn't matter if you eval the namespaced function or include it.
eval("
namespace foo {
function time() {
return 1234;
}
function rand() {
return 5678;
}
}
");
assert (5678 == rand());
assert (5678 == Foo::rand());
assert (1234 == time());
assert (1234 == Foo::time());
Expected result:
----------------
All assertions are true and time() resolves in the last call to
foo\time().
Actual result:
--------------
The last assertion fails, because time() resolves in foo\Foo to \time()
even though foo\time() exists.
--
Edit bug report at https://bugs.php.net/bug.php?id=68541&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=68541&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=68541&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=68541&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=68541&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=68541&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=68541&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=68541&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=68541&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=68541&r=support
Expected behavior: https://bugs.php.net/fix.php?id=68541&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=68541&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=68541&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=68541&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=68541&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=68541&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=68541&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=68541&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=68541&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=68541&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=68541&r=mysqlcfg