I seem to have been a bit too hasty with my previous reply... You meant it wouldn't be able to access the static vars of the inherited Base class?... I think that's a different feature regarding the static access mechanism currently already in place... I'm only suggesting the ability to reference classes dynamically some way...
-- Bart Marco Kaiser wrote:
Hi, this doesnt work because static vars are bound to his class and are not inherited by a child class. maybe this would be work with php6 or a other 5.x version. (Same behavior like the singleton pattern getInstance() abstract class stuff) -- MarcoHi all, I'd like to be able to do the following: <?php class Base { public static $var = 'hello'; public function someFunc() { echo self::$var; // Currently maps to Base::$var echo $this::$var; // Should map to Child::$var } } class Child extends Base { public static $var = 'hello'; } $class = 'Child'; $obj = new $class(); // This works. echo $class::$var; // This doesn't. Should map to Child::$var ?> ...in other words: I'd like to be able to access static class variables from inside an instance of the Base and/or Child classes. I'd also like to be able to access them dynamically. ($className::$variable) The only way to do this at the moment (to my knowledge at least) is to create functions in the Child class that returns its static variables. The downside of this is that those functions most likely will be very common (in my case they are) and should therefore belong in the base class. Hence: $this::$variable At the moment there is no way to access static variables from outside of the class dynamically. As a workaround for this I'm currently creating a temporary instance (new $type()) to access them dynamically with a __get() function in all the derived child classes. There are ways to do it with class constants and the constant functions. But it's not very elegant and class constants can't hold arrays and/or objects. I have no idea what the implications would be. Just thought it would be a nice addition to the language. :) Hope I didn't overlook some existing PHP feature that already allows me to do this. :| Cheers, Bart de Boer
Hi, this doesnt work because static vars are bound to his class and are not inherited by a child class. maybe this would be work with php6 or a other 5.x version. (Same behavior like the singleton pattern getInstance() abstract class stuff) -- MarcoHi all, I'd like to be able to do the following: <?php class Base { public static $var = 'hello'; public function someFunc() { echo self::$var; // Currently maps to Base::$var echo $this::$var; // Should map to Child::$var } } class Child extends Base { public static $var = 'hello'; } $class = 'Child'; $obj = new $class(); // This works. echo $class::$var; // This doesn't. Should map to Child::$var ?> ...in other words: I'd like to be able to access static class variables from inside an instance of the Base and/or Child classes. I'd also like to be able to access them dynamically. ($className::$variable) The only way to do this at the moment (to my knowledge at least) is to create functions in the Child class that returns its static variables. The downside of this is that those functions most likely will be very common (in my case they are) and should therefore belong in the base class. Hence: $this::$variable At the moment there is no way to access static variables from outside of the class dynamically. As a workaround for this I'm currently creating a temporary instance (new $type()) to access them dynamically with a __get() function in all the derived child classes. There are ways to do it with class constants and the constant functions. But it's not very elegant and class constants can't hold arrays and/or objects. I have no idea what the implications would be. Just thought it would be a nice addition to the language. :) Hope I didn't overlook some existing PHP feature that already allows me to do this. :| Cheers, Bart de Boer
-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
