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
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.