thinkphp5 控制器下public,protected,private三者的区别

public:公共 protected:继承 private:私有

请看示例

<?php

namespace app\index\controller;
use think\Controller;
//Test父类
class TestFather extends Controller
{
    public function aaa(){
        return "这是TEST下public控制器";
    }

    protected function bbb(){
        return "这是TEST下protected控制器";
    }

    private function ccc(){
        return "这是TEST下private控制器";
    }

}
<?php

namespace app\index\controller;
//Test子类
class TestSon extends TestFather
{
    public function ddd(){
        return $this->aaa();
    }

    public function eee(){
        return $this->bbb();
    }

    public function fff(){
        return $this->ccc();
    }

}
<?php

namespace app\index\controller;
use think\Controller;

//毫不相干的Test类
class Test extends Controller
{
    public function xxx(){
        return $this->redirect('TestFather/aaa');
    }

    public function yyy(){
        return $this->redirect('TestFather/bbb');
    }
    public function zzz(){
        return $this->redirect('TestFather/ccc');
    }

}

访问TestSon下的ddd函数,页面效果如下:

访问TestSon下的eee函数,页面效果如下:

访问TestSon下的fff函数,页面效果如下:

访问Test下的xxx函数,页面重定向到TestFatcher下的aaa

访问Test下的yyy函数,页面重定向到TestFatcher下的bbb,显示方法不存在

访问Test下的zzz函数,页面重定向到TestFatcher下的ccc,显示方法不存在

通过上面的测试,可以得出结论

public定义的函数能被任何类获取

protected定义的函数只能被继承的子类获取

private定义的函数只能本类获取