Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question already has an answer here:

I can't attain the primary used of Static Visibility. My question is why they used static method & properties in this code?

class PassHash {

    // blowfish
    private static $algo = '$2a';
    // cost parameter
    private static $cost = '$10';

    // mainly for internal use
    public static function unique_salt() {
        return substr(sha1(mt_rand()), 0, 22);
    }

    // this will be used to generate a hash
    public static function hash($password) {

        return crypt($password, self::$algo .
                self::$cost .
                '$' . self::unique_salt());
    }

    // this will be used to compare a password against a hash
    public static function check_password($hash, $password) {
        $full_salt = substr($hash, 0, 29);
        $new_hash = crypt($password, $full_salt);
        return ($hash == $new_hash);
    }

}
share|improve this question

marked as duplicate by MichaelT, GlenH7, Bart van Ingen Schenau, gnat, jwenting Jun 24 at 13:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
Related (and possible duplicates): OO PHP static keyword, should I use it?, Using static in PHP. Please also check out the php + static tags. –  MichaelT Jun 20 at 22:22

1 Answer 1

up vote 0 down vote accepted

Ask yourself if a given method requires any state that is specific to an object instance. If it does not, static is appropriate, since the method is scoped to the class itself, not to the object instance.

static methods are essentially utility methods; they take parameters, and return a value; nothing more.

Consider

public static function check_password($hash, $password) {
    $full_salt = substr($hash, 0, 29);
    $new_hash = crypt($password, $full_salt);
    return ($hash == $new_hash);
}

Does it require any state that is specific to an object instance? Is everything that it needs passed in as parameters? Does it store any state? Does it return an idempotent value?

Also, do you want to be able to call it without requiring a class instance?

If all of these things are true, static scoping is more appropriate than object scoping.

share|improve this answer
    
Hi Thanks I'm 70% enlighten by your answer. can you please specify what do you mean on "utility methods; they take parameters, and return a value". I think non-static function normally do this. –  user1203401 Jun 20 at 21:10
    
Yes, you can do that with non-static methods, but you need to new up an object instance to use them. More info at php.net//manual/en/language.oop5.static.php –  Robert Harvey Jun 20 at 21:10
    
"Does method called rarely?" can you consider this as a basis? –  user1203401 Jun 20 at 21:34
    
How often you call a method isn't really a factor when determining whether or not it should be made static. –  Robert Harvey Jun 21 at 4:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.