0

Possible Duplicate:
PHP: Static and non Static functions and Objects

Why would I use the static key word in php? Is it only to create "good" code or does it have some advantages as well?

function useThisAsStatic(){}

static function useThisAsStatic(){}

public static function useThisAsStatic(){}

public function useThisAsStatic(){}

To clarify the question; all above methods can be used by calling

Object::useThisAsStatic();

And thus suggests that there is no difference in declaring a method to be static.

Community
  • 1
  • 1
Roel Veldhuizen
  • 4,373
  • 8
  • 39
  • 71

4 Answers4

2

Is it only to create "good" code

Actually it's to create bad code.
Read why
And this article is more specified about static (thanks to Gordon)

OZ_
  • 11,972
  • 6
  • 45
  • 66
0

Static functions mean you can call them without instantiating the class (creating the object) first.

Useful in certain situations, but static functions cannot make use of the local members of the class outside of that function.

Evernoob
  • 5,490
  • 8
  • 32
  • 49
0

static means belonging to the Class. You can call a static method or field. Without creating instance of the class.

sushil bharwani
  • 27,522
  • 29
  • 88
  • 122
0
class NewClass {
    public $public_var = "I'm public property!";
    public static $static_var = "I'm static property!";

    public function public_method()
    {
        return "I'm public method!";
    }

    public static function static_method()
    {
        return "I'm static method!";
    }
}

Now you can call NewClass::$static_var and NewClass::static_method() without creating NewClass instance. While to call public_var and public_method you should create new instance of class:

$c = new NewClass();
echo $c->public_var;
echo $c->public_method();
Johnatan
  • 1,258
  • 8
  • 16