-2

我制作了一个由controllers/pages.phpviews/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');

VIEWS/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>
4

2 回答 2

0

您正在调用方法,就像它是属性一样。

改成等等$this->setCopyright$this->setCopyright()

class Page {
    private $title;

    function setCopyright() {
        ...
    }
}

$page = new Page();

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

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

于 2013-01-23T10:25:01.867 回答
0

You are returning this when you should be returning $this.

Switch on error reporting and you will see errors about trying to return this.


Secondly, if you want to call a method, call it:

$this->listCSS();
              ^^

I don't know what you expect that result to be, since listCSS() returns $this (the object you're calling the method of).

于 2013-01-23T10:06:11.443 回答