emehrkay Posted February 1, 2008 Share Posted February 1, 2008 I've been trying different things for about an hour while making no real headway. I want to have a static property, but I need to be able to call it from a variable that defines the class name. class test{ public static foo = 'bar'; } Sometimes I need to access foo statically and other times I need to access it from an instance that has been dynamically created. $class = 'test'; $class::variable; //not possible, you get that double something error $instance = new $class(); $instance->variable; //not possible either because of the static keyword What do I do? Thanx Quote Link to comment https://forums.phpfreaks.com/topic/88826-solved-static-property-question/ Share on other sites More sharing options...
emehrkay Posted February 1, 2008 Author Share Posted February 1, 2008 BTW, I really really dont want to create a method that would return that static property because I am trying to keep it as simple as possible public function getFoo(){ return self::foo; } Quote Link to comment https://forums.phpfreaks.com/topic/88826-solved-static-property-question/#findComment-454961 Share on other sites More sharing options...
emehrkay Posted February 1, 2008 Author Share Posted February 1, 2008 It looks like it is an error that was fixed in 5.3. Adding that method seems to be the best solution. Look in the comments: http://us2.php.net/language.oop5.static Damn, I do not want to add that method just to get to that property Quote Link to comment https://forums.phpfreaks.com/topic/88826-solved-static-property-question/#findComment-454965 Share on other sites More sharing options...
emehrkay Posted February 1, 2008 Author Share Posted February 1, 2008 I caved in and just created a static function that returns the value that i once had in the propery public static function foo(){ return 'bar'; } and I will just access it by doing test::foo(); instead of test::$foo; Quote Link to comment https://forums.phpfreaks.com/topic/88826-solved-static-property-question/#findComment-454969 Share on other sites More sharing options...
448191 Posted February 2, 2008 Share Posted February 2, 2008 Calling test::Foo() is no more dynamic than calling test::FOO (a class constant) or test::$foo (a static property). Quote Link to comment https://forums.phpfreaks.com/topic/88826-solved-static-property-question/#findComment-455974 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.