php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69456 can't set protected / private static methods on ReflectionClass object
Submitted: 2015-04-15 05:51 UTC Modified: 2015-04-15 06:34 UTC
From: cameron at datashovel dot com Assigned:
Status: Not a bug Package: Reflection related
PHP Version: 5.5.23 OS: AWS Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: cameron at datashovel dot com
New email:
PHP Version: OS:

 

 [2015-04-15 05:51 UTC] cameron at datashovel dot com
Description:
------------
I can't seem to set new values on static properties using either setValue or setStaticPropertyValue using ReflectionClass

Test script:
---------------
<?php

$array = array(
    'blah1' => 1,
    'blah2' => 2,
    'blah3' => 3
);

class Blah
{
    public static $blah1 = 4;
    protected static $blah2 = 5;
    private static $blah3 = 6;
}

$rc = new ReflectionClass('Blah');

// brand new ReflectionClass
var_dump($rc);

$static = $rc->getProperties(ReflectionProperty::IS_STATIC);
$obj = $rc->newInstanceWithoutConstructor();
foreach($static as $prop){
    if(!empty($arr[$prop->getName()])){
        $prop->setAccessible(true);
        $prop->setValue($obj, $arr[$prop->getName()]);
        $rc->setStaticPropertyValue($prop->getName(), self::cast($arr[$prop->getName()]));
    }
}
$obj2 = $rc->newInstanceWithoutConstructor();

// instantiate class after ReflectionClass has set all property values
var_dump($obj2);
var_dump($obj2::$blah1);

// class instantiated before ReflectionClass captured ReflectionClass values
// but after we have called "setValue"
var_dump($obj);
var_dump($obj::$blah1);


Expected result:
----------------
either by calling setValue OR by instantiating a new instance AFTER setting the Static properties on the ReflectionClass object I should be able to set static values.

Obviously public static is less of an issue because since I could just go in afterward and set the value on the instance.  But protected and private pose a problem because unless I create special setters / getters I will be unable to set custom values on these properties through reflection.

Actual result:
--------------
object(ReflectionClass)#1 (1) {
  ["name"]=>
  string(4) "Blah"
}
object(Blah)#6 (0) {
}
int(4)
object(Blah)#5 (0) {
}
int(4)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-04-15 05:56 UTC] cameron at datashovel dot com
Still think it's a bug, but found I accidentally introduced code from the project I was working on where I experienced this.

In the test script you can change:

-------FROM-------
$rc->setStaticPropertyValue($prop->getName(), self::cast($arr[$prop->getName()]));

--------TO-------
$rc->setStaticPropertyValue($prop->getName(), self::cast($arr[$prop->getName()]));
 [2015-04-15 05:58 UTC] cameron at datashovel dot com
This is embarassing :)  Just realized both lines are same.  This should fix it.

In the test script you can change:

-------FROM-------
$rc->setStaticPropertyValue($prop->getName(), self::cast($arr[$prop->getName()]));

--------TO-------
$rc->setStaticPropertyValue($prop->getName(), $arr[$prop->getName()]);
 [2015-04-15 06:18 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2015-04-15 06:18 UTC] requinix@php.net
1. You named the array "$array" but try to use "$arr".
2. You can't use setStaticPropertyValue() on an inaccessible property.

Fixing #1 and commenting out that line for #2 gives me the expected output of two "int(1)"s.
http://3v4l.org/SUXK5
 [2015-04-15 06:34 UTC] cameron at datashovel dot com
Ugh, I try to avoid reporting issues late at night for this exact reason.  Thanks for the catch.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 23:01:26 2024 UTC