Jump to content

[SOLVED] PHP5: static vs non-static


Leppy

Recommended Posts

Hello,

 

I've been doing some research on this forum and I haven't really found anything that answer my question which is, what is the difference between static and non-static methods and variables?

 

The main reason why I am asking is because I like to use the syntax class::method() or class::$variable because it makes more sense and also because when I use the class within function I do not have to declare GLOBAL $my_class.

 

Another reason why I'm asking is, in order to be able to use the methods or variable I call using class::method() or class::$variable, I need to specify the "static" keyword all the time otherwise I get a PHP error #2048 Non-static method which I find very annoying.

 

That being said, what is the difference between static and non-static? When should "static" be used and why?

 

Any help is appreciated,

 

Thank you.

 

Leppy-

Link to comment
Share on other sites

class static variables, (module static variables) store information pertaining to class instances. As for methods, the difference between a method being defined as static or not is simply this.

 

static = method is a member of the given class.

non-static = method is a member of a instance of the given class.

 

 

For a simple example of usage...

 

Say you have web crawler class that handles fetching documents in parallel. In that class you may have variables (rules, cookies, domain caches) that need to be shared between all instances of that class, in order to keep your application from doing things that may have already been done. So you declare the variable as static because that variable is common to all instances. Think of it, an instance can see it's class, a class can never see it's instance, so a declared static variable or method gives all instances of a class a way to share common variables, (information) declared static between it's instances.

Link to comment
Share on other sites

Static variables and methods can be accessed without actually having to instantiate the class.

 

Static method

MyClassName::static_method();

 

instead of

$obj = new MyClassName();
$obj->method();

 

I see, I didn't know that. Thanks!

Link to comment
Share on other sites

class static variables, (module static variables) store information pertaining to class instances. As for methods, the difference between a method being defined as static or not is simply this.

 

static = method is a member of the given class.

non-static = method is a member of a instance of the given class.

 

 

For a simple example of usage...

 

Say you have web crawler class that handles fetching documents in parallel. In that class you may have variables (rules, cookies, domain caches) that need to be shared between all instances of that class, in order to keep your application from doing things that may have already been done. So you declare the variable as static because that variable is common to all instances. Think of it, an instance can see it's class, a class can never see it's instance, so a declared static variable or method gives all instances of a class a way to share common variables, (information) declared static between it's instances.

 

Well said, I understand now. Thank you!

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.