php中self和this的用法和区别

this是指向当前对象的指针(姑且用C里面的指针来看吧)
self是指向当前类的指针

1,静态方法里不能用$this->调用

error_reporting(E_ALL);// 将所有错误信息报出来

class Test
{
    public static function test1()
    {
        //$this->test2();// 这样子肯定是报错的,静态方法不能使用$this
        // 如果非要在这里调用test2(),可以使用以下方式,称为方式一
        $instance = new Test();
        $instance->test2();
        // 或者使用更简单的方式,称为方式二(PHP会给出调用警告)
        self::test2();
    }

    public function test2()
    {
        echo 'test static function';
    }
}

Test::test1();


2,在非静态方法中可以用$this->调用静态方法
class Test
{
    public function test1()
    {
        $this->test2();
    }

    static public function test2()
    {
        echo 'test static function';
    }
}
$a = new Test();
$a->test1();