php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63665 Relative namespace import causes getClass() method to throw an exception
Submitted: 2012-12-01 23:10 UTC Modified: 2013-03-19 09:49 UTC
From: dev at jurajsplayground dot com Assigned: dmitry (profile)
Status: Not a bug Package: Reflection related
PHP Version: 5.4.9 OS: Ubuntu
Private report: No CVE-ID: None
 [2012-12-01 23:10 UTC] dev at jurajsplayground dot com
Description:
------------
While importing of namespaced classes via relative namespace directive works when 
creating objects, ReflectionParameter::getClass() has a problem with it and 
throws an exception that such class cannot be found.

Test script:
---------------
namespace Foo\Bar;
use A\B\BInterface;

class Bar implements BarInterface {
	public function __construct()
	{
		echo "Hello!";
	}

	public function getSomething(BInterface $b){}
}

namespace Foo\Bar;
use A\B\BInterface;

interface BarInterface {
	public function getSomething(BInterface $b);
}

namespace Foo\Bar\A\B;

interface BInterface {}

namespace TestNamespace;

$obj = new \Foo\Bar\Bar();
$ref = new \ReflectionMethod("\\Foo\Bar\Bar", "getSomething");
echo current($ref->getParameters())->getClass();

Expected result:
----------------
Should display interface information

Actual result:
--------------
ReflectionException is thrown: Class A\B\BInterface does not exist

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-12-02 05:22 UTC] laruence@php.net
actually, this is not a only reflection specific issue.

<?php
namespace Foo\Bar\A\B;

interface BInterface {}

class Foo implements BInterface {
}

namespace Foo\Bar;
use A\B\BInterface;

class Bar implements BarInterface {
    public function __construct()
    {
        echo "Hello!";
    }

    public function getSomething(BInterface $b){}
}

interface BarInterface {
        public function getSomething(BInterface $b);
}

namespace TestNamespace;

use Foo\Bar\A\B;
$obj = new \Foo\Bar\Bar();
$arg = new \Foo\Bar\A\B\Foo;
$obj->getSomething($arg);


PHP Catchable fatal error:  Argument 1 passed to Foo\Bar\Bar::getSomething() 
must be an instance of A\B\BInterface, instance of Foo\Bar\A\B\Foo given, called 
in /tmp/1.php on line 30 and defined in /tmp/1.php on line 18


dmitry, could you please look at this one?
 [2012-12-02 05:22 UTC] laruence@php.net
-Assigned To: +Assigned To: dmitry
 [2012-12-02 05:52 UTC] laruence@php.net
I have tried to make a fix, but for such situation

use \Foo;  CG(current_import) only hold the Foo name but doesn't hold the global 
name flag..

so, I can not simply resolve the full name of a class name which is imported and 
in a namespace.
 [2013-03-19 09:49 UTC] dmitry@php.net
PHP doesn't support *relative* namespace import. A name from "use" statement is always used as a full namespace name.
 [2013-03-19 09:49 UTC] dmitry@php.net
-Status: Assigned +Status: Not a bug
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Thu Apr 09 04:01:23 2020 UTC