PHP访问包含文件上的父类
关于这个问题的最新消息 我不希望类“BlogPost”访问其在main.php页面上设置的父类变量PHP访问包含文件上的父类,php,class,include,Php,Class,Include,关于这个问题的最新消息 我不希望类“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($p
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
}
}
$page = new BlogPage("index");
if($page->PageExists == true){
include("posts.php");
}else{
include("notfound.php");
}
<?php
$page = new BlogPage("index");
if($page->PageExists == true){
include("posts.php");
} else{
include("notfound.php");
}
?>
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
}
}
$page = new BlogPage("index");
if($page->PageExists == true){
include("posts.php");
}else{
include("notfound.php");
}
<?php
$page = new BlogPage("index");
if($page->PageExists == true){
include("posts.php");
} else{
include("notfound.php");
}
?>
posts.php
$pageTitle = $page->PageTitle;
$posts = new BlogPost();
<?php
$pageTitle = $page->PageTitle;
$posts = new BlogPost($page->PageId);
?>
?>我觉得一类中的变量受到保护如果二类从一类扩展,您将能够:
$two = new classTwo();
$two->functionFromClassOne();
并有权进入课堂
您最好解释确切的用例,以便推荐最佳方法。继承可能不是实现您试图构建的任何内容的最佳方式。如果您想访问父级的类保护和公共变量及函数,那么您将使用parent::static前缀 在您的例子中,如果您想访问classOne的受保护和公共变量以及ClassII中的函数,那么您只需要在ClassII中使用parent::inside 如果您只想在包含的文件上使用classTwo实例化对象,那么您不需要将其声明为全局对象,您只需正常访问它,就像在主文件上声明它的几行下面访问它一样 更新1 您不需要将该变量的作用域定义为全局,因为它已经在脚本的该部分具有该作用域。所以,只需像这样访问它:
// global $page; remove this, no need for it
$pageTitle = $page->PageTitle;
$posts = new BlogPost();
更新2
这是我对第二个问题的建议解决方案:
<?php
class Page{
public $PageExists = false;
public $PageTitle = 'no title';
public $PageId = '0';
// add other options here
// add other parameters to this function
// or pass an array to it
protected function fill($page_id, $page_title){
$this->PageExists = true;
$this->PageId = $page_id;
$this->PageTitle = $page_title;
}
}
class BlogPage extends Page{
function __construct($page){
//some sql to check if page exists
if($page_exists){
parent::fill($fetched['row_id'], $fetched['row_title']);
}
}
}
class BlogPost extends Page {
function __construct($page_id){
//some sql to get the posts that have post_page like $page_id
if($post_exists){
parent::fill($fetched['row_id'], $fetched['row_title']);
}
}
}
?>
然后你可以像下面这样使用你的类
在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
}
}
$page = new BlogPage("index");
if($page->PageExists == true){
include("posts.php");
}else{
include("notfound.php");
}
<?php
$page = new BlogPage("index");
if($page->PageExists == true){
include("posts.php");
} else{
include("notfound.php");
}
?>
关于posts.php
$pageTitle = $page->PageTitle;
$posts = new BlogPost();
<?php
$pageTitle = $page->PageTitle;
$posts = new BlogPost($page->PageId);
?>
您的代码片段将非常棒您如何尝试从ClassOne中检索信息在'classTwo'i us$this->varInClassOne或parent::funcInClassOne中,但这并不是重点!我试了我能找到的一切!关于全局关键字的旁注:当包含一个文件时,它包含的代码将继承包含所在行的变量范围。从该点开始,调用文件中该行可用的任何变量都将在被调用文件中可用。但是,包含文件中定义的所有函数和类都具有全局作用域。-换句话说,不需要在那里使用全局$classOne,一般来说,在执行OOP时,您希望避免使用该关键字sec@MarcM:那么,我已经回答了你的问题,这是我回答的第二部分。您不需要将该变量的作用域定义为全局,因为它已经在脚本的该部分具有该作用域。因此,只需像这样访问$pageTitle=$page->pageTitle;。当我这样做的时候,我开始尝试获取非对象的属性in@Marcvd M:也许您试图获取通知中非对象的属性来自于您处理DB连接和记录获取的方式?如果您给了我们一行代码,告诉我们问题出在哪里,并将这行代码发布给我们,那么我们将能够告诉您确切的问题@首先,这个$page\u footer=$page->footer;,是什么;,BlogPage类上没有页脚公共变量。试着注释掉那一行,看看我们从中得到了什么。你确定如果$page_存在{是真的吗?是的,这是mysql_num_rows$sql,当找到与$PostId匹配的帖子时,这是真的。如果它为假,我将得到页面notfound.php