2009-10-31 106 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); 

无论如何,你必须回到绘图板和清理它。 也许这是太复杂的练习使用类的第一个项目。