【发布时间】:2011-06-17 11:08:05
【问题描述】:
关于问题的新更新
我不想让“BlogPost”类访问它在 main.php 页面上设置的父类变量
class BlogPage {
public $PageExists = false;
public $PageTitle = "no title";
public $PageId = "0";
function __construct($page){
//some sql to check if page exists
if($page_exists){
$this->PageExists = true;
$this->PageTitle = $fetched['row_title'];
$this->PageId = $fetched['row_id'];
}
}
}
class BlogPost extends BlogPage {
function __construct(){
$page_id = $this->PageId;
//some sql to get the posts that have post_page like $page_id
}
}
Main.php 页面
$page = new BlogPage("index");
if($page->PageExists == true){
include("posts.php");
}else{
include("notfound.php");
}
posts.php
$pageTitle = $page->PageTitle;
$posts = new BlogPost();
?>
【问题讨论】:
-
您的代码的 sn-p 会很棒(您如何尝试从 classOne 检索信息)
-
在 'classTwo' 我使用 $this->varInClassOne 或 parent::funcInClassOne 但这真的不是重点!我尝试了我能找到的一切!
-
(sidenote) 关于
global关键字:当一个文件被包含时,它包含的代码继承了包含发生的行的变量范围。从那时起,调用文件中该行的任何可用变量都将在被调用文件中可用。但是,包含文件中定义的所有函数和类都具有全局范围。 - 换句话说,不需要在那里使用global $classOne,而且通常在执行 OOP 时无论如何都希望避免使用该关键字。