Jump to content

Static Functions/Variables


SchweppesAle

Recommended Posts

Hi, I recently proposed a question regarding the negative consequences of Singleton Factory classes:

http://www.phpfreaks.com/forums/index.php/topic,288257.msg1366428.html#msg1366428

 

I then did some additional reading up on static variables since the two are closely related and supposedly is also an example of bad practice in OO programming:

http://giorgiosironi.blogspot.com/2009/11/mocking-static-methods-road-to-hell.html

 

How do you guys feel about this, is there an appropriate time/place for static?  Are they really all that bad?

Link to comment
Share on other sites

I think there are times when static variables/methods do come in handy. You just need to know when to use them. I think it also depends on the specific coding practices for the particular language you're working with as well. For example in a language like ActionScript 3.0 that is almost explicitly Object Oriented it's standard practice to create a class specifically for defining constants.

 

Ex:

 

package
{
    public class Constants
    {
        public static const SOME_CONSTANT:uint = 0;
        ...
    }
}

 

One practical example in PHP for why you would want to use a static property is such: Say you have a User class that has a property id. You want each User object's property id be an incrementing value starting at 0. You could achieve that like so:

 

class User
{
    private static $_nextID = 0;
    
    private $_id;

    public function __construct()
    {
        $this->_id = self::$_nextID++;
    }
}

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.