3

I'm new to PHP and I have an issue I can't seem to fix or find a solution to.

I'm trying to create a helper function that will return an 'object' filled with information pulled from an XML file. This helper function, named functions.php contains a getter method which returns a 'class' object filled with data from an SVN log.xml file.

Whenever I try to import this file using include 'functions.php'; none of the code after that line runs the calling function's page is blank.

What am I doing wrong?

Here is what the functions.php helper method and class declaration looks like:

<?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;
}
9
  • declare global $log_xml, $list_xml; in function getEntry($date){ Commented Oct 10, 2013 at 4:37
  • @TamilSelvan I tried adding your suggestion in the getEntry() function and it still has the same issue. Could it have anything to do with how I'm importing functions.php? Commented Oct 10, 2013 at 4:50
  • include Entry class in functions.php file Commented Oct 10, 2013 at 4:52
  • What exactly is your file layout? Is the getEntry function in the functions.php, the Entry class is a separate file, and the xml loads in the main file? How does your include fit into the execution? Commented Oct 10, 2013 at 4:56
  • @TamilSelvan I'm not sure what you mean by including the Entry class in functions.php, how should I do that? Commented Oct 10, 2013 at 5:10

1 Answer 1

3

I'm not sure what the point of having a separate helper function from the class is, personally I'd combine the two. Something like this

other-file.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;
    }
}

EDIT

In light of the fact OP is new to PHP, I'll revise my suggestion completely. How about ditching the class altogether here? There's hardly any reason to use a class I can see at this point; let's take a look at using an array instead.

I might still move the simplexml_load_file into the helper function though. Would need to see other operations to merit keeping it broken out.

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;
}

other-file.php

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

EDIT

One final thought.. Since you're seemingly searching for a point of interest in the log, then copying out portions of that node, why not just search for the match and return that node? Here's what I mean (a return of false indicates there was no log from that date)

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

Also, what happens if you have multiple log entries from the same date? This will only return a single entry for a given date.

I would suggest using XPATH. There you can throw a single, concise XPATH expression at this log XML and get back an array of objects for all the entries from a given date. What you're working on is a good starting point, but once you have the basics, I'd move to XPATH for a clean final solution.

Sign up to request clarification or add additional context in comments.

4 Comments

I tried implementing your comment but whenever I add the 'require './Entry.php';' line into the method that calls the 'create' function, the page becomes blank. Simply commenting that line out renders the page correctly. I'm lost. :(
I just spotted a syntax error (missing semi-colon in the class I posted), now fixed. To get things working, consider getting rid of the helper function altogether. I'd recommend getting rid of the extra file altogether too, to start out with. Just put the class I posted in your main file, and call the line to parse the xml file and create an instance of the class. Once that's working you can move the class into an external file.
Another thought.., for something this simple, have you considered using an array to represent the entries? I like OOP, but there's not much to the actual object here except 2 public members; arrays could be more appropriate, and likely an easier way to get started. I'll post an alternative solution.
Added another approach, and some further thoughts; best of luck!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.