3

What are the use of static functions in OOP?

How it is differentiated from other function ?

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Constructor should be private here ?
In some cases we write the static functions to create a new instance, since we are not allowing any direct instance creation for that class. In that time we will write constructor as Private. Public constructor + Static function is not the write way to create OOP design I think (If static functions is used for instance creation).
@Sahal As I wrote, in this case the static method is an alternative object constructor. You can create an instance both via new Foo and via Foo::instantiateFromArray, both of which accept different kinds of arguments but result in the same instance. Foo::__construct is the canonical constructor, Foo::instantiateFromArray is an alternative constructor. There's no point in making a constructor private just to force the caller to use a static method instead which instantiates the same object.
3

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);

Comments

1

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.

Comments

0

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";

1 Comment

At least say that you copy and pasted that from php.net/manual/en/language.oop5.static.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.