|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-11-15 02:27 UTC] hans at velum dot net
Description:
------------
When using type hints, the namespaces that were imported (using "use") are not consulted to resolve the type-hinted class names. i.e. if I have a class defined in namespace "foo::bar", it is assumed that *all* classes specified as type hints used in methods in my class are in the "foo::bar" namespace -- regardless of which other namespaces I import.
Reproduce code:
---------------
This is best illustrated with 3 files:
AClass.php:
<?php
namespace proj::a;
class AClass {}
?>
BClass.php:
<?php
namespace proj::b;
use proj::a;
class BClass {
function setA(AClass $a) {}
}
?>
test.php:
<?php
require 'AClass.php';
require 'BClass.php';
$b = new proj::b::BClass();
$b->setA(new proj::a::AClass());
?>
Expected result:
----------------
No error.
Actual result:
--------------
Catchable fatal error: Argument 1 passed to proj::b::BClass::setA() must be an instance of proj::b::AClass, instance of proj::a::AClass given, called in test.php on line 6 and defined in BClass.php on line 5
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Dec 01 12:00:01 2025 UTC |
This is expected. README.namespaces: 7) qualified class names are interpreted as class from corresponding namespace. So "new A::B::C()" refers to class C from namespace A::B. ... <?php namespace A; new A(); // first tries to create object of class "A" from namespace "A" (A::A) // then creates object of internal class "A" ?> Using: use proj::a; with type hint a::AClass. or without 'use' with type hint proj::a::AClass. or use proj::a as x; with type hint x::AClass. Works fine!