【问题标题】:PHP Accessing parent class on an include filePHP访问包含文件上的父类
【发布时间】: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 时无论如何都希望避免使用该关键字。

标签: php class include


【解决方案1】:

如果你想访问父类的protectedpublic变量和函数,那么你将使用parent::静态前缀。

在你的情况下,如果你想在classTwo 中访问classOneprotectedpublic 变量和函数,那么你只需在classTwo 中使用parent::

如果您只想在包含的文件上使用classTwo 实例化对象,那么您不需要将其声明为global,您只需正常访问它,就像在下面声明它的几行访问它一样在主文件上。

更新 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 页面上

<?php
$page = new BlogPage("index");
if($page->PageExists == true){
    include("posts.php");
} else{
    include("notfound.php");
}
?>

在posts.php上

<?php
$pageTitle  = $page->PageTitle;
$posts      = new BlogPost($page->PageId);
?>

【讨论】:

  • @Marc vd M:那么,我已经回答了你的问题,我的第二部分答案。您不需要将该变量的范围定义为global,因为它已经在脚本的该部分具有该范围。因此,只需像这样访问它$pageTitle = $page-&gt;PageTitle;
  • 当我这样做时,我会尝试获取非对象的属性
  • @Marc vd M:也许您的Trying to get property of non-object in 通知来自您处理数据库连接和记录获取的方式?如果您告诉我们问题出在哪里,并将该行发布给我们,那么我们将能够告诉您确切的问题。
  • 注意:试图在第 2 行的 serverpath/posts.php 中获取非对象的属性 - 第 2 行 = $pageTitle = $page->PageTitle;
  • @Marc vd M:首先,这是什么$page_footer = $page-&gt;Footer;BlogPage 类上没有Footer 公共变量。试着注释掉那一行,看看我们从中得到了什么。
【解决方案2】:

如果 classTwo 从 classOne 扩展,您将能够做到:

$two = new classTwo();
$two->functionFromClassOne();

并有权访问该课程。

解释确切的用例可能对您有好处,因此可以推荐最佳方法。也许继承并不是实现您要构建的任何东西的最佳方式。

【讨论】:

  • 是的,我会在几秒钟内发布一个 sn-p
【解决方案3】:

我觉得你在 classOne 中保护了你的变量

【讨论】:

  • 你确定 if($page_exists){ 是真的吗?
  • 是的,它是 mysql_num_rows($sql),当找到与 $PostId 匹配的帖子时,它是真的。如果它是假的,我会得到页面 notfound.php
猜你喜欢
  • 1970-01-01
  • 2015-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-29
相关资源
最近更新 更多