Help with classes in php
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);
}
}
}
I'm a newbie in working with classes , i don't understand so much , 开发者_如何学Pythoni want to implement and to study to work with classes this example of mvc structure , but i have a problem , with function set I'm saving into array , some information and then to sent inside class template , when i using inside function __construct I'm sending with set() function that have the role to save data into $this_template object, it's working ok , but when i'm creating a new function in this class or extended class , is not working ...
The question is - how to do , when i create a function in Controller class , to set in array the value that i need , to work with them inside class template :) thanks very much for helping ..and sorry for my English
class Template {
protected $variables = array();
function set($name,$value) {
$this->variables[$name] = $value;
}
function render(){
extract($this->variables); print_r($this->variables);
}
}
I need with function set() from Class Controller to export data inside class Template , and why when i creating a function inside Class Controller ex :
function functionName() {
$data=array('a','b');
$this->set('data',$data);
}
and inside class Template
, i putting print_r($this->variables);
, and the array is empty
__construct($controller, $action) {
$this->_controller = ucfirst($controller);
...}
What exactly is the type of variable $controller? is is a string, perhaps? and should it be called "controller_name"?
$model = ucfirst($controller);/* Conecting the model class*/
...
$modelName = ucfirst($model).'Model';
Isn't the ucfirst redundant on the second line?
new $modelName;
should be
$this->somevariable = new Someclass($modelName);
anyway, you have to go back to the drawing board and clean this up. Perhaps this is too complex of an exercise for your first project using classes.
精彩评论