Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static::staticFunctionName()

Tags:

php

I know what self::staticFunctionName() and parent::staticFunctionName() are, and how they are different from each other and from $this->functionName.

But what is static::staticFunctionName()?

like image 940
shealtiel Avatar asked Nov 08 '10 01:11

shealtiel


People also ask

How static functions are called?

If the keyword static is prefixed before the function name, the function is called a static function. It is often called a method. A method is a group of variables and statements that functions together as a logical unit. Like fields, methods can have modifiers (like private, public, or static).

How do you make a function static in C++?

Static Member Functions in C++ To create a static member function we need to use the static keyword while declaring the function. Since static member variables are class properties and not object properties, to access them we need to use the class name instead of the object name.

What is static function in Java?

A static method in Java is a method that is part of a class rather than an instance of that class. Every instance of a class has access to the method. Static methods have access to class variables (static variables) without using the class's object (instance). Only static data may be accessed by a static method.


1 Answers

It's the keyword used in PHP 5.3+ to invoke late static bindings.
Read all about it in the manual: http://php.net/manual/en/language.oop5.late-static-bindings.php


In summary, static::foo() works like a dynamic self::foo().

class A {
    static function foo() {
        // This will be executed.
    }
    static function bar() {
        self::foo();
    }
}

class B extends A {
    static function foo() {
        // This will not be executed.
        // The above self::foo() refers to A::foo().
    }
}

B::bar();

static solves this problem:

class A {
    static function foo() {
        // This is overridden in the child class.
    }
    static function bar() {
        static::foo();
    }
}

class B extends A {
    static function foo() {
        // This will be executed.
        // static::foo() is bound late.
    }
}

B::bar();

static as a keyword for this behavior is kind of confusing, since it's all but. :)

like image 149
deceze Avatar answered Oct 13 '22 10:10

deceze