duanfanta6741 2016-11-16 04:53
浏览 37
已采纳

PHP调用两个函数,但保存第一个变量?

i am trying to set a title with the function below and then call another function so i can render the view but after I cannot see the title. here is my code:

snippet from another file:

$indexPage = new View();
$indexPage->setPageTitle('This is the title');
$indexPage->render('index');

This is my view class:

class View {

    private $title;

    public function render($file) {
        require '/view/header.php';
        require '/view/'.$file.'.php';
        require '/view/footer.php';
    }

    public function setPageTitle($title) {
        $this->title = $title;
    }

}

then I would access it on my page:

index.php:

<?php echo $this->title ?>

but it does not show.

  • 写回答

2条回答 默认 最新

  • doulupian8725 2016-11-16 05:01
    关注

    Yes it will not show. If you created View object in index.php as mentioned in code snipet then you call <?php echo $indexPage->title ?> Otherwise when you rendered view template you have to pass data like most MVC does.

    Note: make title variable public if you want to access it directly otherwise write another public function and return the private member.

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

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测