php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #36687 ReflectionParameter::getDeclaringClass returns wrong result
Submitted: 2006-03-10 22:19 UTC Modified: 2006-03-12 18:22 UTC
From: elias at adaptiveinstance dot com Assigned: helly (profile)
Status: Closed Package: Feature/Change Request
PHP Version: 5.1.2 OS: *
Private report: No CVE-ID: None
 [2006-03-10 22:19 UTC] elias at adaptiveinstance dot com
Description:
------------
The method ReflectionParameter::getDeclaringClass() always return the wrong result. I should reaturn the reflection object of the declaring class and not that of the typehint class.
Further this method has been introduced somewhere between php 5.0.5 and 5.1.3, but the documentation doesn't mention it.

Reproduce code:
---------------
class OtherClass {}
class MyClass
{
	function myMeth(OtherClass $param0, $param1){}
}

$class = new ReflectionClass('MyClass');
$params = $class->getMethod('myMeth')->getParameters();
var_dump($params[0]->getDeclaringClass());
var_dump($params[0]->getClass());
var_dump($params[1]->getDeclaringClass());
var_dump($params[1]->getClass());


Expected result:
----------------
object(ReflectionClass)#2 (1) {
  ["name"]=>
  string(10) "MyClass"
}
object(ReflectionClass)#2 (1) {
  ["name"]=>
  string(10) "OtherClass"
}
object(ReflectionClass)#2 (1) {
  ["name"]=>
  string(10) "MyClass"
}
NULL

Actual result:
--------------
object(ReflectionClass)#2 (1) {
  ["name"]=>
  string(10) "OtherClass"
}
object(ReflectionClass)#2 (1) {
  ["name"]=>
  string(10) "OtherClass"
}
NULL
NULL

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-03-11 17:08 UTC] helly@php.net
getClass() is an alias for getDeclaringClass() in 5.1 and will be dropped in later versions.

What you want is a new method maybe named getParameterClass() or getTypeHintClass().
 [2006-03-11 18:30 UTC] elias at adaptiveinstance dot com
Alias or not, getDeclaringClass does not return the 'declaring' class and i don't think someone expects this.

getTypeHintClass sounds good.
 [2006-03-11 18:34 UTC] helly@php.net
It definitively returns which class declared the parameter.
You are in RelfectionParameter here not in RelfectionTypeHint which doesn't exist.
 [2006-03-11 20:07 UTC] elias at adaptiveinstance dot com
In that case i must understand anythink completely wrong or...

---code
class Foo
{
	function myMeth($param0){}
}

$class = new ReflectionClass('Foo');
$params = $class->getMethod('myMeth')->getParameters();
var_dump($params[0]->getDeclaringClass());

---output
NULL
---end

...NULL is the declaring class.
 [2006-03-12 00:00 UTC] helly@php.net
In that case you found an error besides your request.
 [2006-03-12 16:39 UTC] helly@php.net
After discussing this issue again we found that indeed
getClass() should return the typehine class if any and getDeclaringClass() should return where the parameter is defined which is the class of the method if it is a method and NULL otherwise.

Fixed in head now, fix for 5.1.3 will come, too.
 [2006-03-12 18:22 UTC] helly@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 16 16:08:09 2025 UTC