Jump to content

[SOLVED] static statements within a class


seanlim

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/180721-solved-static-statements-within-a-class/
Share on other sites

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.

@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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.