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
https://forums.phpfreaks.com/topic/192645-static-functionsvariables/
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++;
    }
}

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.