dongzhuo3059 2016-08-14 16:16
浏览 8
已采纳

PHP如何在MVC模式中从布局渲染函数?

I've a code with im working for.

  class View
{
    public $model;
    public $function;
    public $content;

    public function __construct( $model )
    {
        $this->model = $model;
        $this->templateLoader( get_class( $this->model ));
    }

    public function templateLoader( $model )
    {
        $template       = "View/Layout/" . $model . ".tpl";

        if ( file_exists( $template ))
        {
            $this->getContent( $template );
            $this->searchForTags();
            $this->runFunction();
        }
    }

    public function getContent( $template )
    {
        $this->content = file_get_contents( $template );
    }

    public function searchForTags()
    {
        $start = strpos( $this->content, '<%')+strlen('<%');
        $end = strpos( $this->content, '%>');
        $part = $end - $start;

        if ( $part > 0)
            $this->function = substr($this->content, $start, $part);
    }

    public function runFunction()
    {
        if ( $this->function )
        {
            var_dump( $this->model->{$this->function} );
        }
    }
}

This class load my template regarding classname. In the template if i have function example:

<td class="right error"> <% getLoginValidatorMsg() %></td>

i want to run it and replace into the value before it shows in my template. Somehow i cant run above function. Error:

Notice: Undefined property: LoginForm::$ getLoginValidatorMsg() in /media/htdocs/ver1/loginmanager/init/View.php on line 54

If i try to run:

$this->model->getLoginValidatorMsg();

will return me the value what i looking for.

  • 写回答

3条回答 默认 最新

  • duanao6704 2016-08-14 16:50
    关注

    It appears from the error you have a space between $ and getLoginValidatorMsg so you need to remove that space using a correctly placed trim() or just str_replace() on the whole thing. Also, it is saying it's looking for a property so you need to add () on your $this->model->{$this->function} so it turns it to a method:

    $this->model->{$this->function}();
    

    Alternatively, you can use call_user_func() / call_user_func_array().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。