PHP包括外部方法和类

PHP包括外部方法和类,php,object,methods,import,include,Php,Object,Methods,Import,Include,我是PHP新手,我有一个问题似乎无法解决或找到解决方案 我正在尝试创建一个helper函数,该函数将返回一个“对象”,其中包含从XML文件中提取的信息。这个名为functions.php的助手函数包含一个getter方法,该方法返回一个“class”对象,其中填充了SVN log.xml文件中的数据 每当我尝试使用include'functions.php'导入此文件时该行运行调用函数页面后的所有代码均为空 我做错了什么 下面是functions.php助手方法和类声明的内容: <?php

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

我正在尝试创建一个helper函数,该函数将返回一个“对象”,其中包含从XML文件中提取的信息。这个名为functions.php的助手函数包含一个getter方法,该方法返回一个“class”对象,其中填充了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;
}

我不确定从类中分离出一个helper函数有什么意义,我个人会将两者结合起来。像这样的

other file.php

require './Entry.php';
$oLogEntry = Entry::create($date, 'svn_log.xml');
echo $oLogEntry->date;
echo $oLogEntry->revision;
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;
    }
}
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;
}
require './entry.php';
$aLogEntry = Entry::create($date, 'svn_log.xml');
echo $aLogEntry['date'];
echo $aLogEntry['revision'];
Entry.php

require './Entry.php';
$oLogEntry = Entry::create($date, 'svn_log.xml');
echo $oLogEntry->date;
echo $oLogEntry->revision;
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;
    }
}
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;
}
require './entry.php';
$aLogEntry = Entry::create($date, 'svn_log.xml');
echo $aLogEntry['date'];
echo $aLogEntry['revision'];
编辑

鉴于OP是PHP的新手,我将完全修改我的建议。把这门课全部扔在这儿怎么样?在这一点上,我几乎没有理由使用我能看到的类;让我们来看看使用数组的方式。

不过,我可能仍然会将
simplexml\u load\u文件
移动到helper函数中。将需要看到其他行动值得保持它的爆发

entry helper.php

require './Entry.php';
$oLogEntry = Entry::create($date, 'svn_log.xml');
echo $oLogEntry->date;
echo $oLogEntry->revision;
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;
    }
}
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;
}
require './entry.php';
$aLogEntry = Entry::create($date, 'svn_log.xml');
echo $aLogEntry['date'];
echo $aLogEntry['revision'];
other file.php

require './Entry.php';
$oLogEntry = Entry::create($date, 'svn_log.xml');
echo $oLogEntry->date;
echo $oLogEntry->revision;
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;
    }
}
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;
}
require './entry.php';
$aLogEntry = Entry::create($date, 'svn_log.xml');
echo $aLogEntry['date'];
echo $aLogEntry['revision'];
编辑

最后一个想法。。既然您似乎在日志中搜索一个关注点,然后复制出该节点的一部分,为什么不搜索匹配项并返回该节点呢?以下是我的意思(返回
false
表示从该日期起没有日志)

另外,如果同一日期有多个日志条目,会发生什么情况?这将只返回给定日期的单个条目


我建议使用。在这里,您可以在这个日志XML中抛出一个简单的XPATH表达式,并为给定日期的所有条目返回一个对象数组。您正在进行的工作是一个很好的起点,但一旦您掌握了基本知识,我将转向XPATH,以获得一个干净的最终解决方案。

declare
global$log\u xml,$list\u xml
在函数getEntry($date){
@TamilSelvan中,我尝试在getEntry()中添加您的建议函数,它仍然有相同的问题。这可能与我如何导入函数.php有关吗?在functions.php文件中包含Entry类您的文件布局到底是什么?是functions.php中的getEntry函数,Entry类是一个单独的文件,xml加载在主文件中吗?include如何适合执行@TamilSelvan我不知道你在functions.php中包含Entry类是什么意思,我应该怎么做?我尝试实现你的注释,但每当我在调用“create”函数的方法中添加“require.”/Entry.php“;”行时,页面就会变为空白。只需对该行进行注释,页面就会正确呈现。我迷路了:(我刚刚发现一个语法错误(我发布的类中缺少分号)为了使事情正常工作,考虑完全摆脱辅助函数。我建议完全摆脱这个额外的文件,开始。只需把我在你的主文件中张贴的类,并调用该行来解析XML文件并创建类的实例。ernal文件。另一个想法..,对于这么简单的事情,你考虑过使用数组来表示条目吗?我喜欢OOP,但除了2个公共成员之外,这里的实际对象没有太多;数组可能更合适,并且可能更容易开始。我将发布一个替代解决方案。添加了另一种方法,以及其他一些方法呃,祝你好运!