Sahal
Sahal

Reputation: 4136

What are the use of static functions in OOP?

What are the use of static functions in OOP?

How it is differentiated from other function ?

Upvotes: 3

Views: 10007

Answers (4)

deceze
deceze

Reputation: 522081

As a very simple, somewhat contrived example:

class Foo {

    protected $bar = null;
    protected $baz = null;

    public function __construct($bar, $baz) {
        $this->bar = $bar;
        $this->baz = $baz;
    }

    public static function instantiateFromArray(array $data) {
        return new self($data['bar'], $data['baz']);
    }

}

$foo = new Foo('bar', 'baz');
// or
$foo = Foo::instantiateFromArray(array('bar' => 42, 'baz' => 'nine'));

In this case the static function serves as an alternative constructor, allowing you to construct the object from an array of data instead of separate arguments.

Generally, static functions provide functionality around an object without needing to instantiate it. There are many uses for that. At the very least, entirely static classes which are never instantiated can be used to bundle functions and related data together, which in itself makes code cleaner. That's where they differ from normal functions: normal functions cannot save "external" data (without using globals, which you don't want to do), static class methods can save data in static class properties.

Upvotes: 6

BabloBko
BabloBko

Reputation: 69

Static functions are used to invoke a class code when none of its instance exists( in more purer oop languages).Static functions can change static variables.

Upvotes: 1

Hamid
Hamid

Reputation: 1760

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).

For compatibility with PHP 4, if no visibility declaration is used, then the property or method will be treated as if it was declared as public.

Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside the method declared as static.

Static properties cannot be accessed through the object using the arrow operator ->.

Calling non-static methods statically generates an E_STRICT level warning.

Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object.

class Foo
{
    public static $my_static = 'foo';

    public function staticValue() {
        return self::$my_static;
    }
}

class Bar extends Foo
{
    public function fooStatic() {
        return parent::$my_static;
    }
}


print Foo::$my_static . "\n";

$foo = new Foo();
print $foo->staticValue() . "\n";
print $foo->my_static . "\n";      // Undefined "Property" my_static 

print $foo::$my_static . "\n";
$classname = 'Foo';
print $classname::$my_static . "\n"; // As of PHP 5.3.0

print Bar::$my_static . "\n";
$bar = new Bar();
print $bar->fooStatic() . "\n";

Upvotes: 0

alex
alex

Reputation: 490233

You do not need to instantiate the object to use static methods/properties.

Because of this, they can not store their state in the object.

They are often used as a way to namespace related methods, e.g.

echo str::truncate($str, 100);

Upvotes: 3

Related Questions