arnoschaefer Posted August 27, 2007 Share Posted August 27, 2007 Hi, I have this bit of code: class bla { static public function foo () { } } $classname = "bla"; $classname::foo (); in my installation of PHP 5.2.4RC1 (OpenSuSE 10.2), this works fine. On another server with PHP 5.2.2, this results in Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in /www/htdocs/w0088307/blog/version/test/tt2.php on line 9 (PAAMAYIM_NEKUDOTAYIM appears to mean double colon (). Can anyone tell me if this is a change between version 5.2.2 and 5.2.4 (I did not find anything in the ChangeLog), or if this is a configuration problem? Is the above code legal at all? Best Regards, Arno Quote Link to comment https://forums.phpfreaks.com/topic/66904-static-methods-different-behavior/ Share on other sites More sharing options...
lemmin Posted August 27, 2007 Share Posted August 27, 2007 try $classname->foo(); Quote Link to comment https://forums.phpfreaks.com/topic/66904-static-methods-different-behavior/#findComment-335387 Share on other sites More sharing options...
trq Posted August 27, 2007 Share Posted August 27, 2007 Looks legal to me, I use this type of stuff quite a bit without issue. I can't say Ive tried with static methods though. Changes between 5.2.2 and 5.2.4 should merely be bug fixes, not feature changes. I'm sorry, but I can't think of any config option that may cause this either. Quote Link to comment https://forums.phpfreaks.com/topic/66904-static-methods-different-behavior/#findComment-335394 Share on other sites More sharing options...
arnoschaefer Posted August 27, 2007 Author Share Posted August 27, 2007 > try $classname->foo(); $classname is not an object, but a string: Fatal error: Call to a member function foo() on a non-object in /www/htdocs/w0088307/blog/version/test/tt2.php on line 9 Quote Link to comment https://forums.phpfreaks.com/topic/66904-static-methods-different-behavior/#findComment-335406 Share on other sites More sharing options...
lemmin Posted August 27, 2007 Share Posted August 27, 2007 Oh, sorry, I didn't pay attention to what you were trying to do. What about bla::foo(); I think that is what you are looking for. Quote Link to comment https://forums.phpfreaks.com/topic/66904-static-methods-different-behavior/#findComment-335415 Share on other sites More sharing options...
trq Posted August 27, 2007 Share Posted August 27, 2007 Try... ${classname}::foo (); Quote Link to comment https://forums.phpfreaks.com/topic/66904-static-methods-different-behavior/#findComment-335420 Share on other sites More sharing options...
lemmin Posted August 27, 2007 Share Posted August 27, 2007 Oh, are you trying to use variable variables? If so, this should work: $$classname::foo(); Quote Link to comment https://forums.phpfreaks.com/topic/66904-static-methods-different-behavior/#findComment-335432 Share on other sites More sharing options...
trq Posted August 27, 2007 Share Posted August 27, 2007 Oh, are you trying to use variable variables? If so, this should work: $$classname::foo(); No. Just trying to use a variable's value as an object reference. It should be legal. Quote Link to comment https://forums.phpfreaks.com/topic/66904-static-methods-different-behavior/#findComment-335433 Share on other sites More sharing options...
arnoschaefer Posted August 27, 2007 Author Share Posted August 27, 2007 > What about bla::foo(); I think that is what you are looking for. Well, the whole point of using a variable $classname is that I don't know which class I will be using in advance. Otherwise I would obviously use the classname directly as you suggest. But thanks anyway. Quote Link to comment https://forums.phpfreaks.com/topic/66904-static-methods-different-behavior/#findComment-335664 Share on other sites More sharing options...
arnoschaefer Posted August 27, 2007 Author Share Posted August 27, 2007 > Try... > > Code: > > ${classname}::foo (); Does not work either. Still the same error message. Quote Link to comment https://forums.phpfreaks.com/topic/66904-static-methods-different-behavior/#findComment-335666 Share on other sites More sharing options...
trq Posted August 27, 2007 Share Posted August 27, 2007 I'm at home now using 5.2.2 I get the same error as you. It must be a caveat with static methods. This.... <?php class bla { public function foo () { echo "foo!"; } } $class = 'bla'; $obj = new $class; $obj->foo (); ?> works as expected, but obviously its not what you want. Sorry, but I can't seem to help. Quote Link to comment https://forums.phpfreaks.com/topic/66904-static-methods-different-behavior/#findComment-335683 Share on other sites More sharing options...
lemmin Posted August 27, 2007 Share Posted August 27, 2007 Though I can't seem to find it on their website, I found something that claimed this to be part of the changelog: "Added possibility to call static class members using variables." It would seem you can't do it in previous version. Quote Link to comment https://forums.phpfreaks.com/topic/66904-static-methods-different-behavior/#findComment-335698 Share on other sites More sharing options...
arnoschaefer Posted August 27, 2007 Author Share Posted August 27, 2007 > $class = 'bla'; > $obj = new $class; > $obj->foo (); Yes, that was what I used as a workaround so far, but I just found a hint in the PHP doc on the "scope resolution operator" (double colon) and they had another workaround that is somewhat more elegant and does not require instantiating an object: eval ("$classname::foo ();"); or, with an additional parameter and return value: eval ("\$result = $classname::foo (\$parameter);"); (note the escaped $ signs). There was a discussion that $class::foo() does not work, but why it suddenly started working in version 5.2.4 is still an interesting question. Maybe it was regarded as a bug fix. Unfortunately, there does not seem to be a change log yet for the 5.2.4 release candidates. Thanks anyway for your help and effort. Quote Link to comment https://forums.phpfreaks.com/topic/66904-static-methods-different-behavior/#findComment-335707 Share on other sites More sharing options...
arnoschaefer Posted August 27, 2007 Author Share Posted August 27, 2007 Though I can't seem to find it on their website, I found something that claimed this to be part of the changelog: "Added possibility to call static class members using variables." It would seem you can't do it in previous version. Yes, that's most likely it. Thanks a lot for the info. Quote Link to comment https://forums.phpfreaks.com/topic/66904-static-methods-different-behavior/#findComment-335720 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.