Bug #68541 [Opn->Dup]: namespace fallback for functions unexpected when defined after usage in class.

From: Date: Tue, 31 May 2016 20:22:21 +0000
Subject: Bug #68541 [Opn->Dup]: namespace fallback for functions unexpected when defined after usage in class.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-201372@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=68541&edit=1 ID: 68541 Updated by: cmb@php.net Reported by: markus at malkusch dot de Summary: namespace fallback for functions unexpected when defined after usage in class. -Status: Open +Status: Duplicate Type: Bug Package: Class/Object related Operating System: Linux PHP Version: 5.5.19 -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: > This is probably a duplicate of bug #64346. Indeed. Thanks. Previous Comments: ------------------------------------------------------------------------ [2015-01-29 10:32:39] gen dot work at gmail dot com This is probably a duplicate of bug #64346. ------------------------------------------------------------------------ [2014-12-03 12:17:18] markus at malkusch dot de 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 this bug report at https://bugs.php.net/bug.php?id=68541&edit=1

« previous php.bugs (#201372) next »