php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36337 ReflectionProperty fails to return correct visibility
Submitted: 2006-02-09 12:12 UTC Modified: 2006-02-13 15:50 UTC
From: denis at edistar dot com Assigned: iliaa (profile)
Status: Closed Package: Class/Object related
PHP Version: 5.1.2 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: denis at edistar dot com
New email:
PHP Version: OS:

 

 [2006-02-09 12:12 UTC] denis at edistar dot com
Description:
------------
I have a base abstract class that defines a property to be 
protected and that also checks this runtime using the 
reflection api. 
 
My purpose is to enforce the extending class to declare 
that property as protected too. 
 
The reflection api returns me wrong information. It 
returns always that the property is protected (as defined 
in the base class), ignoring the definition of the 
extending class. 

Reproduce code:
---------------
abstract class enum {
    protected $_values;

    public function __construct() {
        $property = new ReflectionProperty(get_class($this),'_values');
        if(!$property->isProtected()) {
            throw new Exception("Property _values must be declared as protected");
        }
    }

}

final class myEnum extends enum {
    public $_values = array(
           0 => 'No value',
           1 => 'Value n.1',
           2 => 'Value n.2',
           4 => 'Value n.4'
       );
}

$x = new myEnum();


Expected result:
----------------
Fatal error: Uncaught exception 'Exception' with message 
'Property _values must be declared as protected' 
  

Actual result:
--------------
No exception is thrown 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-02-09 19:10 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Parent constructor returns information about the parent class, no bug here.
 [2006-02-09 19:33 UTC] tony2001@php.net
Ilia, the fix is similar to the fix applied to #36308.
 [2006-02-10 09:18 UTC] denis at edistar dot com
The ReflectionProperty class is correctly checking for myEnum->_values property (not for enum->_values) but it returns the visibilty of enum->_values instead of myEnum->_values.

get_class($this) in the enum constructor returns correctly myEnum, so the line "$property = new ReflectionProperty(get_class($this),'_values');" is equivalent to writing "$property = new ReflectionProperty('myEnum','_values');".

The problem remains also if i write this code outside the classes definitions:

$property = new ReflectionProperty('myEnum','_values');
if($property->isProtected()) {
    throw new Exception("Property _values must be declared as
protected");
}

Thank you,
Denis
 [2006-02-13 15:50 UTC] iliaa@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-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 12:01:29 2024 UTC