PHP模板函数不返回数据

时间:2013-01-23 10:03:17

标签: php function

我制作了一个由 controllers / pages.php views / pages.php 组成的基本PHP模板系统。 视图使用控制器使用应该来自控制器的数据填充HTML,但是某些数据未从控制器到观看次数我在下面标注了代码,注释为“#==> NOT WORKING ”任何带有此类评论的内容都不会返回任何数据观点关于可能原因的任何建议?

<?php

# THIS IS CONTROLLERS/PAGES.PHP

class Page {
    private $title, $stylesheets=array(), $javascripts=array(), $body;

    # ==> NOT WORKING
    function Site() {
        $site = "http://dev.site.tld/";
        return $site;
    }

    function Page() {
        $this->addCSS('css/main.css');
        $this->addCSS('css/font-awesome.css');
        $this->addJavascript('js/jquery.js');
        $this->addJavascript('js/application-build102121.js');
    }

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

    # ==> NOT WORKING
    function setCopyright() {
         $copyright_year = date("Y");
         return $copyright_year;
    }

    # ==> NOT WORKING
    function setAppName() {
         $app_name = "Latte";
         return $app_name;
    }

    function addCSS($path) {
        $this->stylesheets[] = $path;
    }

    # ==> NOT WORKING
    function listCSS($path) {   
        $data = Site(); 
        foreach ($this->stylesheets as $stylesheet) {
             echo '<link href="' . $data . $stylesheet . '" rel="stylesheet" type="text/css" />' . "\n";
        }
        return $this;   
    }

    # ==> NOT WORKING
    function listJS($path) {
        $data = Site();
        foreach ($this->javascripts as $javascript) {
             echo '<script src="' . $data . $javascript . '" type="text/javascript" defer="defer"></script>' . "\n";
         }  
        return $this;
    }

    function addJavascript($path) {
        $this->javascripts[] = $path;
    }


    function startBody() {
        ob_start();
    }

    function endBody() {
        $this->body = ob_get_clean();
    }

    function render($path) {
        ob_start();
        include($path);
        return ob_get_clean();
    }
}

Error_Reporting返回

Notice: Undefined property: Page::$listCSS in /home/adminis1/public_html/dev/views/pages.php on line 15

Notice: Undefined property: Page::$listJS in /home/adminis1/public_html/dev/views/pages.php on line 17

Notice: Undefined property: Page::$setCopyright in /home/adminis1/public_html/dev/views/pages.php on line 25

调用数据的方式 的index.php

<?php
require_once('controllers/pages.php');
$page = new Page;
$page->setTitle('Home');
$page->startBody();
?>

<img src="test-img.jpg" />

<?php
$page->endBody();
echo $page->render('views/pages.php');

查看/ PAGES.PHP

<?php error_reporting(E_ALL); ?>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title><?php echo $this-setAppName; ?> - <?php echo $this->title; ?></title>

    <!-- view port meta -->
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">


    <!--[if lt IE 9]>
        <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

    <?php echo $this->listCSS; ?>

    <?php echo $this->listJS; ?>

</head>
<body>

    <?php echo $this->body; ?>

    <footer>
        <p>Copyright &copy; <?php echo $this->setCopyright; ?> </p>
    </footer>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

当您应该返回this时,您将返回$this

启用错误报告,您会看到有关尝试return this的错误。


其次,如果您想调用 方法调用

$this->listCSS();
              ^^

我不知道你期望得到什么结果,因为listCSS()返回$this(你正在调用方法的对象)。

答案 1 :(得分:0)

您正在调用类似于属性的方法。

$this->setCopyright更改为$this->setCopyright(),依此类推。

class Page {
    private $title;

    function setCopyright() {
        ...
    }
}

$page = new Page();

$title是一个属性,可通过$page->title访问它 setCopyright()是一个函数,通过$page->setCopyright()访问它(带括号)。

顺便说一下,在定义对象构造函数时使用 function __construct() ,它比使用 function Page() 更好。