2017-06-17 37 views
-1

你好有人幫助我,我使用$時,有一個問題在我的作弄..that這個時候不是在目標內容.. 我dashboardController.php我的代碼是

<?php 
    class dashboardController extends BaseController{ 
    public function index($name=''){ 
     $this->view->loadView('dashboard/index'); 
    } 

} 

,並在我的baseController.php文件中代碼如下所示

<?php 
class BaseController{ 
    public function loadView($viewName){ 
     $this->view = new View($viewName); 

    } 
} 

,並在我的view.php文件中的代碼看起來像這樣

<?php 
    class View{ 
    public function __construct($viewName);{ echo " i am form view to 
    render 


    } 
} 

但我得到電子RROR使用$這個時候不是在對象中的內容 但在另一個文件夾中登錄無影去無差錯的成功,請幫助我

+0

您是否已經靜態調用該方法,而沒有首先實例化對象?例如'dashboardController :: index()'? – sjdaws

+0

不,我沒有調用這樣的方法,但每當我調用像父:: _構造函數的父構造函數,它也給了我錯誤,當我調用像父:: SS()相同的類的下一個方法;它調好了 – subash

+0

,同時調用父類的方法,我們必須實例化對象,我不認爲如此不能我們direclty調用父類方法從子類 – subash

回答

0

試試這個:

dashboardController

<?php 

class dashboardController extends BaseController 
{ 
    public function index($name = '') 
    { 
     // Call loadView(), because this controller doesn't have a method 
     // called loadView() it'll fall back to loadView() in the BaseController 
     $this->loadView('dashboard/index'); 

     // Get the viewName from the view, will be 'dashboard/index' 
     echo $this->view->getViewName(); 
    } 
} 

BaseController

<?php 

class BaseController 
{ 
    // The view, protected so it can be accessed by children via $this->view 
    protected $view; 

    // Load a view, protected so it can only be accessed by children 
    // or within this controller 
    protected function loadView($viewName) 
    { 
     // Create a new view instance 
     $this->view = new View($viewName); 
    } 
} 

查看

<?php 

class View 
{ 
    // The loaded view, private so it can't be changed by an external class 
    private $viewName; 

    public function __construct($viewName) 
    { 
     $this->viewName = $viewName; 
    } 

    // Retrieve the view name, public so anything can access it 
    public function getViewName() 
    { 
     return $this->viewName; 
    } 
} 
+0

謝謝先生我正試圖理解 – subash

+0

先生我想告訴你我的一些路由代碼你會檢查它並糾正它,如果錯誤並給我一些想法如何利用你可以給我您的電子郵件ID – subash

+0

將其添加到您的原始問題中,當我有機會時我會更新答案,我假設您正在靜態調用控制器 – sjdaws

相關問題