【发布时间】:2014-05-25 00:35:52
【问题描述】:
好的,我有一个 index.php:
<?php
require 'php/stdlib.php';
$site->page->render();
foreach($page as $var => $value) {
echo $var ." is ". $value." <br/>";
}
?>
站点和页面的 obj 创建位于 stdlib 文件中,显然可以正常工作 -for each- 循环打印出来:
name is welcome
headers is inc/index_h.php
footers is inc/index_f.php
contents is inc/welcome.php
它表明对象已创建。 我还做了一个 var dump 并得到了正确的结果 这是网站---页面---渲染:
public function render_page(){
$this->page->render();
}
这里是页面---渲染:
public function render(){
include $this->headers;
include $this->contents;
include $this->footers;
}
然而脚本的结果如下:
未定义变量:
还有
试图获取非对象的属性: 这两个错误都指向我在页眉的包含文件中使用的 $page 对象:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?php echo $page->name; ?></title>
<script src="/scripts/jquery.js"></script>
</head>
<body>
错误实际上是在 html 标题标签中打印出来的,而不是在屏幕上,这意味着我必须在浏览器上使用 View Source 才能看到它 使用包含时如何使 $page 对象可见 我通常很擅长自己找到答案,但是这件事让我困惑了两天。(我在寻找答案时学到了很多其他的东西,所以我想并不是所有的东西都丢失了)如果有人可以帮助我,我将不胜感激它。
可能应该添加页面和站点对象在stdlib.php中实例化如下
$site = new csite();
site_ini($site);
$page = new cpage("welcome");
$site->setPage($page);
【问题讨论】:
标签: php class object scope members