seanlim Posted November 8, 2009 Share Posted November 8, 2009 Hey guys, Anybody knows if the static statement can be used as such? class MyClass{ static{ //blah blah } } I remember seeing that syntax somewhere in another OOP language, but it doesn't seem to work in PHP. What I want is to run some statements within that block, e.g. declaring static arrays on multiple lines. Other than making a call to a static function, is there any other way to do this? Quote Link to comment https://forums.phpfreaks.com/topic/180721-solved-static-statements-within-a-class/ Share on other sites More sharing options...
Alex Posted November 8, 2009 Share Posted November 8, 2009 What do you mean "declaring static arrays on multiple lines"? Can you explain what you're trying to accomplish a bit better? Quote Link to comment https://forums.phpfreaks.com/topic/180721-solved-static-statements-within-a-class/#findComment-953453 Share on other sites More sharing options...
The Little Guy Posted November 8, 2009 Share Posted November 8, 2009 http://php.net/manual/en/language.oop5.static.php class MyClass{ public static function foo(){ //blah blah } } Quote Link to comment https://forums.phpfreaks.com/topic/180721-solved-static-statements-within-a-class/#findComment-953518 Share on other sites More sharing options...
Alex Posted November 8, 2009 Share Posted November 8, 2009 http://php.net/manual/en/language.oop5.static.php class MyClass{ public static function foo(){ //blah blah } } Other than making a call to a static function, is there any other way to do this? Clearly the OP is looking for something different. Quote Link to comment https://forums.phpfreaks.com/topic/180721-solved-static-statements-within-a-class/#findComment-953519 Share on other sites More sharing options...
seanlim Posted November 8, 2009 Author Share Posted November 8, 2009 @AlexWD "declaring static arrays on multiple lines" isn't what I'm trying to achieve, it is merely an example. @The Little Guy I've read that page previously, but could not find any thing useful. I've only dug up the "external initialization" solution in one of the comments. The code above is supposedly called "static initialization blocks" in Java and is perfectly valid syntactically. This url discusses its use in Java: http://isagoksu.com/2009/development/java/understanding-static-blocks/ And since I have found the name of this feature, I have also found that there isn't a similar feature in PHP yet. http://www.stubbles.org/archives/15-.html The only solution seems to be external initialization, as mentioned earlier. Thanks anyway, I was just hoping for a quick answer since I'm more familiar with OOP in Java. Quote Link to comment https://forums.phpfreaks.com/topic/180721-solved-static-statements-within-a-class/#findComment-953520 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.