访问对象变量

时间:2017-03-13 20:13:49

标签: php codeigniter-3

class MY_Controller extends CI_Controller
{

    public $username;

    function __construct()
    {
        parent::__construct();
        $this->username = "User";
    }
}
class Post extends MY_Controller
{

    public function index()
    {        
        $data = array(
            'name'=>'CodeIgniter',
            'username' => $username
        );

        $this->template->load('post/index', $data);
    }
}

我无法访问$username课程中的Post,如何解决此问题?

1 个答案:

答案 0 :(得分:1)

如果 MY_Controller 具有公共用户名属性,则会扩展 CI_Controller ,以及发布类扩展< strong> MY_Controller 然后从发布类使用中访问用户名属性:

$this->username