php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48336 ReflectionProperty::getDeclaringClass() does not work with redeclared propertie
Submitted: 2009-05-19 20:19 UTC Modified: 2009-05-21 16:55 UTC
From: Markus dot Lidel at shadowconnect dot com Assigned:
Status: Closed Package: Reflection related
PHP Version: 5.3CVS-2009-05-19 (snap) OS: Linux
Private report: No CVE-ID: None
 [2009-05-19 20:19 UTC] Markus dot Lidel at shadowconnect dot com
Description:
------------
This is a duplicate of BUG #48286 because there is no way to reopen it and i don't know if someone sees my last comments on this bug. The ReflectionProperty::getDeclaringClass() does report wrong class in derived classes. Always the first occurence of protected and public properties is returned and not the class from which the property gets its value. This is true with static and nonstatic properties.

This patch fixes the issue:

--- php_reflection.c.ori        2008-12-31 12:17:42.000000000 +0100
+++ php_reflection.c    2009-05-15 02:04:51.000000000 +0200
@@ -4125,6 +4125,10 @@
                        break;
                }
                ce = tmp_ce;
+               if (ce == tmp_info->ce) {
+                       /* it's a redeclared property, so no further inheritance needed */
+                       break;
+               }
                tmp_ce = tmp_ce->parent;
        }

Reproduce code:
---------------
<?php

class A {
}

class B extends A {
  static protected $prop;
}

class C extends B {
  static protected $prop;
}

class D extends C {
}

class E extends D {
}

class F extends E {
  static protected $prop;
}

$class = 'A';
for($class = 'A'; $class <= 'F'; $class ++) {
  print($class.' => ');
  try {
    $rp = new ReflectionProperty($class, 'prop');
    print($rp->getDeclaringClass()->getName());
  } catch(Exception $e) {
    print('N/A');
  }
  print("\n");
}
?>

Expected result:
----------------
A => N/A
B => B
C => C
D => C
E => C
F => F


Actual result:
--------------
A => N/A
B => B
C => B
D => B
E => B
F => B

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-05-21 16:05 UTC] lbarnaud@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.


 [2009-05-21 16:55 UTC] Markus dot Lidel at shadowconnect dot com
Thank you very much, with the latest snapshot everything works fine!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 09:01:32 2024 UTC