1

Hey I have a class like this

class derp{

public $$ini;

public static function regIni($ini){

derp::$$ini = 'test';

}

}

And then in another file I have

core::regIni('register');

And then somewhere else I use

core::$register;

This produces an error

public $$ini 

is not valid code but if I leave it without I can not set

core::$$ini

How do I fix this?

Please note that $$ini is a variable variable meaning the value of $ini is actuely the variable name so $ini = 'registry' then $$ini actualy means $registry.

Robin
  • 13
  • 1
  • 4
  • It's not doable. Static properties can only be defined at the parsing stage. Neither `ReflectionProperty` or `ReflectionClass::setStaticPropertyValue`, nor `runkit_*` functions are currently capable (intended) to create static class properties. Sorry. – mario Mar 23 '11 at 14:27
  • @mario Thanks for the clear comment, I would put this as the accepted answer if I could but its commen. I geuss i'll just have to drop the static class en make a normal one. – Robin Mar 23 '11 at 14:37

4 Answers4

3

Why not use access methods for setting and getting class data?

class derp {

    protected static $_data = array(); 

    public static function regIni($ini, $value) {
         derp::$_data[$ini] = $value;   
    }

    public static function getIni($ini, $default = NULL) {
        return isset(derp::$_data[$ini]) ? derp::$_data[$ini] : $default;
    }
}
biakaveron
  • 5,493
  • 1
  • 16
  • 20
  • +1 for beating me to it, I ended up trying magic methods first, only to realize they don't go well with static. – Peter Lindqvist Mar 23 '11 at 13:29
  • So now this would derp:getIni('register') it would now if register would be a class now I would have to go $class = derp::getIni('register') and then $class->function();? this is not really an an anwser to my question just a diffrent solution it does I still can't dynamicly create variables for the class. – Robin Mar 23 '11 at 13:31
  • Now you can use `derp::setIni('register', 'some value');` and `$register = derp::getIni('register');`. – biakaveron Mar 23 '11 at 13:47
  • That seems like a lot more then necasery would't it me be much easier to have core::register->function instead of having having to copy register in a diffrent variable. This method you posted would be fine for variables and stuff but I don't want do create new variables for classes I have in my main class – Robin Mar 23 '11 at 13:49
  • @biakaveron This is a really important part of my code and should be done propaly. There writing a reg and get functions should be avoided as it will just be more work later on. – Robin Mar 23 '11 at 14:01
  • Php doesnt copy object variables, just creates a link to it. But I still have no thoughts about what do you want :) – biakaveron Mar 24 '11 at 08:17
1

So, this is not an overly helpful answer, as I can just conclude that it's currently not possible.

  • You cannot define a new ReflectionProperty("derp", "static_prop") for example and attach it. It's really for introspection only.
  • $c = new ReflectionClass("derp"); and $derp->setStaticPropertyValue("p", 123); is not working either. The properties need to be predefined still.
  • And lastly, neither can the runkit_* functions help with this task. They are intended for changing methods mainly.
  • Same for classkit.

I'm not aware of other such PECL extensions, but that wouldn't be useful as general solution anyway. So for current PHP versions you cannot add static class properties after the parsing stage.

mario
  • 144,265
  • 20
  • 237
  • 291
0

take a look at magic methods __set and __get

julioc
  • 109
  • 5
0

Mario said:

It's not doable. Static properties can only be defined at the parsing stage. Neither ReflectionProperty or ReflectionClass::setStaticPropertyValue, nor runkit_* functions are currently capable (intended) to create static class properties. Sorry

Geuss i'l settle for a work around then. Made an array $ini and loaded the values into there derp:$ini['base']['key']

Thanks for the help,

Robin

Robin
  • 13
  • 1
  • 4