php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76788 Incorrect function namespace resolution
Submitted: 2018-08-23 23:22 UTC Modified: 2018-08-24 09:59 UTC
From: syl dot fabre at gmail dot com Assigned:
Status: Duplicate Package: *General Issues
PHP Version: 7.3.0-beta2 OS: eval.org
Private report: No CVE-ID: None
 [2018-08-23 23:22 UTC] syl dot fabre at gmail dot com
Description:
------------
PHP 7.1.20-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Jul 25 2018 10:06:40) ( NTS ) I don't have access to the latest update 7.1.21

The namespace resolution for a function seems to be cached somehow: if you call a function, then eval code to override the function in a namespace then the evaled code won't be run. If you don't call the function before evaling the code, then it's run.



Test script:
---------------
run php test2.php with the line commented
run php test2.php without the line commented

# test.php
<?php

namespace Test;

Class Hello
{

    public function test()
    {
        var_dump('unqualified:' . time());
        if(function_exists('\Test\time')){
           var_dump('fully-qualified:' . \Test\time());
        }
        else{
           var_dump('fully-qualified:not found');
        }
    }

}


# test2.php
<?php

include __DIR__ . '/test.php';

$test = new Test\Hello();

$test->test(); // toggle comment this line

eval('
namespace Test;

function time(){return \'foo\';}
');

$test->test();

Expected result:
----------------
with the line commented:
string(15) "unqualified:foo"
string(19) "fully-qualified:foo"

with the line uncommented:
string(22) "unqualified:1535066193"
string(25) "fully-qualified:not found"
string(22) "unqualified:foo"
string(19) "fully-qualified:foo"

Actual result:
--------------
with the line commented:
string(15) "unqualified:foo"
string(19) "fully-qualified:foo"

with the line uncommented:
string(22) "unqualified:1535066193"
string(25) "fully-qualified:not found"
string(22) "unqualified:1535066193"
string(19) "fully-qualified:foo"


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-08-23 23:40 UTC] Wes dot example at example dot org
This is the expected behavior. It's called fallback to root scope.
https://wiki.php.net/rfc/fallback-to-root-scope-deprecation
Deprecation is probably never going to happen. Just be sure you have the functions defined before you call them.
 [2018-08-24 00:19 UTC] levim@php.net
This is known behavior, but I'm not sure it's really *intentional* in the sense that it was voted on or otherwise approved or designed.
 [2018-08-24 08:52 UTC] syl dot fabre at gmail dot com
I've run some additional tests: https://3v4l.org/IMmoO

Looks like the behavior is different between PHP and HHVM.

I understand the fallback to root scope mecanism but looks like here it's more "stick to root scope once fallen back". The resolution is done only once, the first time.

FYI, I've found this issue using the clock mock of Symfony PHPUnit Bridge (https://symfony.com/doc/current/components/phpunit_bridge.html#time-sensitive-tests)
 [2018-08-24 09:09 UTC] syl dot fabre at gmail dot com
-Operating System: ubuntu 16.04 +Operating System: eval.org -PHP Version: 7.1.21 +PHP Version: 7.3.0-beta2
 [2018-08-24 09:09 UTC] syl dot fabre at gmail dot com
Reproducible with php 7.3.0-beta2 at eval.org (https://3v4l.org/IMmoO)
 [2018-08-24 09:59 UTC] nikic@php.net
-Status: Open +Status: Duplicate
 [2018-08-24 09:59 UTC] nikic@php.net
Duplicate of bug #64346.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Oct 11 06:01:27 2024 UTC