【问题标题】:Help with classes in php帮助 php 中的类
【发布时间】:2009-10-31 19:18:55
【问题描述】:
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 时,我使用 set() 函数发送,该函数具有将数据保存到 $this_template 对象的作用,它工作正常,但是当我在此类或扩展类中创建新函数不起作用...

问题是 - 当我在 Controller 类中创建一个函数时,如何在数组中设置我需要的值,以便在类模板中使用它们:) 非常感谢您的帮助..对不起我的英语

class Template {
    protected $variables = array();


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


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

我需要使用类控制器中的函数 set() 来导出类模板中的数据,以及为什么当我在类控制器中创建函数时 例如:

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

class Template里面,我放print_r($this->variables);,数组是空的

【问题讨论】:

  • 您的问题是什么?我不明白。
  • 看看 az Zend Framework 以了解 PHP 中的 MVC
  • 我没有那么多时间,我正在做一个项目.. 而此时我正在研究 mvc 结构并使用类
  • 你用蹩脚的英语问了一些非常含糊的问题。如果您发布您想要开始工作的代码、您想要做什么以及为什么它不起作用,这将有所帮助。然后我们就会更好地了解发生了什么。
  • 好的,我会尽量写得更明确一些

标签: php class object


【解决方案1】:
__construct($controller, $action) {

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

变量 $controller 的类型究竟是什么? is 是一个字符串,也许?应该叫“controller_name”吗?

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

第二行的ucfirst不是多余的吗?

new $modelName;

应该是

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

无论如何,您必须回到绘图板并清理它。 对于您使用类的第一个项目来说,这可能是一个过于复杂的练习。

【讨论】:

    猜你喜欢
    • 2011-10-27
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 2014-06-15
    • 2011-01-10
    • 2010-12-30
    相关资源
    最近更新 更多