new self と new static の違い

New self vs. new static stackoverflow.com

self は記述されたクラスに束縛されるのに対して、static は呼び手のクラスに束縛される。

class A {
    public static function getSelfName() {
        return get_class(new self());
    }

    public static function getStaticName() {
        return get_class(new static());
    }
}

class B extends A {
    public static function getParentName() {
        return get_class(new parent());
    }
}

echo A::getSelfName(),PHP_EOL;   //A
echo B::getSelfName(),PHP_EOL;   //A

echo A::getStaticName(),PHP_EOL; //A
echo B::getStaticName(),PHP_EOL; //B

echo B::getParentName(),PHP_EOL; //A

メソッドを呼び出したクラスの名前の取得には get_called_class() 関数を使うことができる。

class A {
    static public function getCalledClass() {
        return get_called_class();
    }
}

class B extends A {

}

echo A::getCalledClass(),PHP_EOL; //A
echo B::getCalledClass(),PHP_EOL; //B

__CLASS__ 定数は記述されたクラスの名前をあらわす。

class A {
    static public function getClassName() {
        return __CLASS__;
    }
}

class B extends A {

}

echo B::getClassName(),PHP_EOL; //A