> What is a static method/function in oop php?

What is a static method/function in oop php?

Posted at: 2014-12-18 
it is hard to answer this question easyly because it means something slightly different based on the language. If I were to put it in the most general terms which will probably wildly vary from person to person:

A static variable is a variable that is shared across all instances of a class.

A static method is a method that can be called on a class and usually does not require the class to be instantiated.

Again if I were to pick three different languages I would give three different answers.

Wikipedia might also help a bit to define these things.

http://en.wikipedia.org/wiki/Method_(com... http://en.wikipedia.org/wiki/Static_vari...

class A {

public static function Foo() {

echo 'Static method called!';

}

};

// Calling a static method

A::Foo();

// Result: 'Static method called!'

// Simple as that!

You don't have to instantiate a class to call a static method that belongs to it.

These functions are useful because you can create things like singletons.

class Singleton {

$this.m_hData; // whatever data you have there

$self::Instance = 0; // public static

public static function Instance () {

if ($self::Instance == 0) {

$self::Instance = new Singleton();

}

return $self::Instance;

};

};

Singleton::Instance()->m_hData = "My Name";

// My PHP is a little rusty since i don't use it anymore.