【问题标题】:PHP include external method and classPHP包含外部方法和类
【发布时间】:2013-10-17 16:45:53
【问题描述】:

我是 PHP 新手,遇到一个问题,我似乎无法修复或找到解决方案。

我正在尝试创建一个辅助函数,该函数将返回一个“对象”,其中填充了从 XML 文件中提取的信息。这个名为 functions.php 的辅助函数包含一个 getter 方法,该方法返回一个“类”对象,其中填充了来自 SVN log.xml 文件的数据。

每当我尝试使用include 'functions.php'; 导入此文件时,在该行运行之后,调用函数的页面都不会是空白的。

我做错了什么?

下面是 functions.php 辅助方法和类声明的样子:

<?php
        $list_xml=simplexml_load_file("svn_list.xml");
        $log_xml=simplexml_load_file("svn_log.xml");


class Entry{

    var $revision;
    var $date;
}

function getEntry($date){
      $ret = new Entry;
      foreach ($log_xml->logentry as $logentry){
        if ($logentry->date == $date){
            $ret->date = $logentry->date;
            $ret->author = $logentry->author;
        }
    }
    return $ret;
}

【问题讨论】:

  • function getEntry($date){中声明global $log_xml, $list_xml;
  • @TamilSelvan 我尝试在 getEntry() 函数中添加您的建议,但仍然存在同样的问题。它与我导入functions.php的方式有什么关系吗?
  • 在functions.php文件中包含Entry类
  • 您的文件布局到底是什么?是不是functions.php中的getEntry函数,Entry类是一个单独的文件,而xml加载在主文件中?您的包含如何适合执行?
  • @TamilSelvan 我不确定你在functions.php中包含Entry类是什么意思,我应该怎么做?

标签: php object methods import include


【解决方案1】:

我不确定从类中拥有一个单独的辅助函数有什么意义,我个人会将两者结合起来。像这样的

其他文件.php

require './Entry.php';
$oLogEntry = Entry::create($date, 'svn_log.xml');
echo $oLogEntry->date;
echo $oLogEntry->revision;

Entry.php

class Entry
{
    public $revision;
    public $date;
    public $author;

    public static function create($date, $file) {
        $ret = new Entry;
        $xml = simplexml_load_file($file);
            foreach($xml->logentry as $logentry) {
            if($logentry->date == $date) {
                $ret->date     = $logentry->date;
                $ret->author   = $logentry->author;
                $ret->revision = $logentry->revision;
            }
        }
        return $ret;
    }
}

编辑

鉴于 OP 是 PHP 的新手,我将彻底修改我的建议。在这里完全放弃课程怎么样?在这一点上,几乎没有任何理由使用我可以看到的类。让我们看看使用数组来代替。

不过,我可能仍会将simplexml_load_file 移动到辅助函数中。需要查看其他操作以使其保持中断。

entry-helper.php

function getEntry($date, $file) {
    $log_xml = simplexml_load_file($file);
    $entry   = array();
    foreach($log_xml->logentry as $logentry) {
        if($logentry->date == $date) {
            $entry['date']     = $logentry->date;
            $entry['author']   = $logentry->author;
            $entry['revision'] = $logentry->revision;
        }
    }
    return $entry;
}

其他文件.php

require './entry.php';
$aLogEntry = Entry::create($date, 'svn_log.xml');
echo $aLogEntry['date'];
echo $aLogEntry['revision'];

编辑

最后一个想法.. 既然您似乎是在日志中搜索兴趣点,然后复制该节点的部分内容,为什么不直接搜索匹配项并返回该节点?这就是我的意思(返回false 表示该日期没有日志)

function getEntry($date, $file) {
    $log_xml = simplexml_load_file($file);
    foreach($log_xml->logentry as $logentry) {
        if($logentry->date == $date) {
          return $logentry;
    return false;
}

另外,如果您有多个来自同一日期的日志条目会怎样?这只会返回给定日期的单个条目。

我建议使用XPATH。在那里,您可以在此日志 XML 中抛出一个简洁的 XPATH 表达式,并从给定日期获取所有条目的对象数组。您正在做的工作是一个很好的起点,但是一旦您掌握了基础知识,我就会转向 XPATH 以获得一个干净的最终解决方案。

【讨论】:

  • 我尝试实现您的评论,但每当我添加 'require './Entry.php';'进入调用“create”函数的方法,页面变为空白。只需注释该行即可正确呈现页面。我迷路了。 :(
  • 我刚刚发现了一个语法错误(我发布的课程中缺少分号),现在已修复。为了使事情正常进行,请考虑完全摆脱辅助功能。我建议也完全摆脱额外的文件,开始。只需将我发布的类放在您的主文件中,然后调用该行来解析 xml 文件并创建该类的实例。一旦这样做了,您就可以将类移动到外部文件中。
  • 另一个想法..,对于这么简单的事情,您是否考虑过使用数组来表示条目?我喜欢 OOP,但除了 2 个公共成员之外,这里的实际对象并不多;数组可能更合适,并且可能是一种更简单的入门方式。我将发布一个替代解决方案。
  • 添加了另一种方法,以及一些进一步的想法;祝你好运!
猜你喜欢
  • 1970-01-01
  • 2014-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-16
  • 2011-07-22
相关资源
最近更新 更多