23

I guess there may not be any difference but personal preference, but when reading various PHP code I come across both ways to access the methods class.

What is the difference:

class Myclass
{
    public static $foo;

    public static function myMethod ()
    {
        // between:
        self::$foo;
        // and
        MyClass::$foo;
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
raveren
  • 17,799
  • 12
  • 70
  • 83

2 Answers2

43

(Note: the initial version said there was no difference. Actually there is)

There is indeed a small diference. self:: forwards static calls, while className:: doesn't. This only matters for late static bindings in PHP 5.3+.

In static calls, PHP 5.3+ remembers the initially called class. Using className:: makes PHP "forget" this value (i.e., resets it to className), while self:: preserves it. Consider:

<?php
class A {
    static function foo() {
        echo get_called_class();
    }
}
class B extends A {
    static function bar() {
        self::foo();
    }
    static function baz() {
        B::foo();
    }
}
class C extends B {}

C::bar(); //C
C::baz(); //B
Artefacto
  • 96,375
  • 17
  • 202
  • 225
  • 1
    Important correction/addition: the `static` keyword was introduced for this purpose. Add a `const FOO = __CLASS__;` to each A, B and C, and make `A::foo()` print both `self::FOO` and `static::FOO`. Only the latter will actually be correct for `C::baz()`. – janmoesen Sep 02 '10 at 11:20
  • 1
    @Artefacto which would you recommend for a typical application that doesn't use late static bindings? Would you opt for `self::` or `MyClass::`? Which did you suggest performed more efficiently? Thanks – Lea Hayes Jul 19 '11 at 22:05
  • 7
    @Lea It's mostly a matter of style, but `self::` can be more handy when refactoring. – Artefacto Jul 19 '11 at 22:50
  • @Artefacto thanks for clarifying that, I'll stick to `self::` then as that is what I have been using. I came across this question and was curious if there would be a useful performance boost. – Lea Hayes Jul 19 '11 at 23:53
  • 1
    To clarify @janmoesen's comment: **The following is wrong:**: *"`self::` forwards static calls"*. NO, the linked document shows that `self::` **does not forward** static calls. If you want forwarding (Late Static Binding), use `static::`. This would be seen if `C` also defined `static function foo() { echo "Truth"; }`. If LSB occurred, `C::bar` would echo "Truth". But it won't. (Tested) So how does `C::bar` echo "C"? When `C::bar` is about to execute, `C` is stored in some internal php location: *"late static bindings work by storing the class named in the last "non-forwarding call"."* – ToolmakerSteve Apr 25 '19 at 18:59
  • 1
    ... so this example demonstrates the first half of LSB - the storage of classname `C`. However `self::foo` does not use that classname, when finding which version of `foo` to execute. `static::foo` would do the correct search for function version. **If this explanation goes over your head, then you probably don't have code where it matters**. Use `self::` for convenience. Until you encounter a situation where it doesn't do what you want. – ToolmakerSteve Apr 25 '19 at 19:03
1

With self you can use it within the class and with the "MyClass", as you have, you can reference it externally:

$instance = new Myclass();
$variable = $instance::$foo
gabe3886
  • 4,235
  • 3
  • 27
  • 31