israel love php israel love php - 1 month ago 10
PHP Question

when using self, parent, static and how?

If by now I understood a little in ststic Now I realize I do not understand anything. I'm so confused and I struggle to understand and I can not. Someone can explain this program when using self, parent, static and how
All the smallest change I do changes the result without that I can not understand what's going on.
thanks a lot ..

the code from http://docs.php.net/language.oop5.late-static-bindings

<?php
class A {
public static function foo() {
static::who();
}

public static function who() {
echo __CLASS__."\n";
}
}

class B extends A {
public static function test() {
A::foo();
parent::foo();
self::foo();
}

public static function who() {
echo __CLASS__."\n";
}
}
class C extends B {
public static function who() {
echo __CLASS__."\n";
}
}

C::test();
?>


The out put are:

A
C
C

Answer

You'll need to understand the concept of Late Static Binding, which determines when an identifier is bound to code/data. You can tell PHP to bind it early (self::) or later (static::).

Slimming the example down to two classes we get:

class A {
    public static function foo() {
        self::who(); // PHP binds this to A::who() right away
        static::who();  // PHP waits to resolve this (hence, late)!
    }

    public static function who() {
        echo __CLASS__."\n";
    }
}

class B extends A {
    public static function test() {
       self::foo();
    }

    public static function who() {
        echo __CLASS__."\n";
    }
}

B::test();