#43302 [Opn->Bgs]: Type hints do not respect imported namespaces

From: Date: Thu, 15 Nov 2007 11:14:37 +0000
Subject: #43302 [Opn->Bgs]: Type hints do not respect imported namespaces
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-119680@lists.php.net to get a copy of this message
ID: 43302 User updated by: hans at velum dot net Reported By: hans at velum dot net -Status: Open +Status: Bogus Bug Type: Scripting Engine problem Operating System: Gentoo PHP Version: 5.3CVS-2007-11-15 (snap) New Comment: Ok, I think I see my error :) I was assuming that "use proj::a;" would bring the classes within that namespace into my local scope, but clearly that is not the case. Thanks for clarification. I could have sworn that I had an example where that did work ... but if I did, I'll open another bug to discuss the inconsistency :) Previous Comments: ------------------------------------------------------------------------ [2007-11-15 09:43:09] felipensp at gmail dot com 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! ------------------------------------------------------------------------ [2007-11-15 02:27:33] 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 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=43302&edit=1

« previous php.bugs (#119680) next »