2009-10-31 64 views
0
class Controller { 

    protected $_controller; 
    protected $_action; 
    protected $_template; 

    public $doNotRenderHeader; 
    public $render; 

    function __construct($controller, $action) { 

     $this->_controller = ucfirst($controller); 
     $this->_action = $action; 
     $model = ucfirst($controller);/* Conecting the model class*/ 
     $this->doNotRenderHeader = 0; 
     $this->render = 1; 
     $modelName = ucfirst($model).'Model'; 
     new $modelName; 
     $this->_template = new Template($controller,$action); 

    } 

    function set($name,$value) { 
     $this->_template->set($name,$value); 
    } 


    function __destruct() { 
     if ($this->render) { 
      $this->_template->render($this->doNotRenderHeader); 
     } 
    } 

} 

我在使用類一個新手,我不明白這麼多,我想實現,並研究與類MVC結構的這個例子的工作,但我有一個問題,與我保存到陣列的功能集,一些信息,然後以類模板內發送,當我使用內部函數__construct我與集發送()函數,必須將數據存入$ this_template對象的角色,它的好的工作,但是當我創建這個類或擴展類的新功能,是不工作...幫助與PHP類

的問題是 - 怎麼做,當我創建Controller類的功能,以擺陣我需要的價值,與他們一起在t班內工作emplate :) 非常感謝幫助..和對不起我從類控制器英語

class Template { 
    protected $variables = array(); 


function set($name,$value) { 
    $this->variables[$name] = $value; 
} 


    function render(){ 
     extract($this->variables); print_r($this->variables); 
    } 
} 

我需要的功能集()導出數據類模板裏面,爲什麼當我創建類控制器內部的功能 例如:

function functionName() { 
    $data=array('a','b'); 
    $this->set('data',$data); 
} 

和內部class Template,我把print_r($this->variables);,且數組爲空

+0

北京時間什麼你的問題?我不明白。 – 2009-10-31 19:20:33

+0

看看az Zend框架,瞭解PHP中的MVC – erenon 2009-10-31 19:22:33

+0

我沒有那麼多時間,我在一個項目上工作..而在這個時候,我正在學習mvc結構,並與類一起工作 – mIRU 2009-10-31 19:30:00

回答

0
__construct($controller, $action) { 

     $this->_controller = ucfirst($controller); 
...} 

變量$控制器的類型究竟是什麼?是一個字符串,也許?並且它應該被稱爲「controller_name」?

$model = ucfirst($controller);/* Conecting the model class*/ 
... 
$modelName = ucfirst($model).'Model'; 

第二行不是ucfirst冗餘嗎?

new $modelName; 

應該

$this->somevariable = new Someclass($modelName); 

無論如何,你必須回到繪圖板和清理它。 也許這是太複雜的練習使用類的第一個項目。