douchang6770 2016-03-10 00:39
浏览 44
已采纳

php - 用数组创建一个对象

I am trying to parse an XML file. I want to create a project object that has instances such as title,date,version and an array of files that hold all the files within the project. Everything seems to work such as the title,date,and version.

I checked by printing them out to see the results. However, when I try printing out the array to see if the contents are correct, nothing happens. I'm not sure where I'm going wrong.

<?php

require_once('project.php');
require_once('files.php');


function parse()
{
    $svn_list = simplexml_load_file("svn_list.xml");
    $dir = $svn_list->xpath("//entry[@kind = 'dir']");


    foreach ($dir as $node) {
        if (strpos($node->name, '/') == false) {

            $endProject = initProject($node);

        }
    }

    for ($x = 0; $x <= 7; $x++) {
        echo $endProject->fileListArray[$x]->name . "<br />
";
    }

}

function initProject($node){

    $project = new project();
    $project->title = $node->name;
    $project->date = $node->commit->date;
    $project->version = $node->commit['revision'];

    initFiles($node,$project);

    return $project;

}

function initFiles($project){

    $svn_list = simplexml_load_file("svn_list.xml");
    $file = $svn_list->xpath("//entry[@kind ='file']/name[contains(., '$project->title')]/ancestor::node()[1]");
    //$file = $svn_list->xpath("//entry[@kind='file']/name[starts-with(., '$project->title')]/..");

    foreach($file as $fileObject){
        $files = new files();
        $files->size = $fileObject->size;
        $files->name = $fileObject->name;
        array_push($project->fileListArray, $files);
    }

}

echo $endProject->fileListArray prints out "Array" 7 times. However echo $endProject->fileListArray[$x]->name does not print anything out.

I'm not sure if the array is just not being initialized or if I'm parsing the XML file incorrectly.

<?xml version="1.0" encoding="UTF-8"?>
<lists>
<list
        path="https://subversion....">
    <entry
            kind="file">
        <name>.project</name>
        <size>373</size>
        <commit
                revision="7052">
            <author></author>
            <date>2016-02-25T20:56:16.138801Z</date>
        </commit>
    </entry>
    <entry
            kind="file">
        <name>.pydevproject</name>
        <size>302</size>
        <commit
                revision="7052">
            <author></author>
            <date>2016-02-25T20:56:16.138801Z</date>
        </commit>
    </entry>
    <entry
            kind="dir">
        <name>Assignment2.0</name>
        <commit
                revision="7054">
            <author></author>
            <date>2016-02-25T20:59:11.144094Z</date>
        </commit>
    </entry>
  • 写回答

2条回答 默认 最新

  • doushouj966020 2016-03-11 00:54
    关注

    Your function definition:

    function initFiles( $project )
    

    Your function call:

    initFiles( $node, $project );
    

    So, the function use $node as $project, but $node doesn't have ->fileListArray property array, so your array_push() fails.

    And, in the future, don't forget to activate error checking in our php code:

    error_reporting( E_ALL );
    ini_set( 'display_errors', 1 );
    

    With error checking, your original code output this error:

    PHP Warning: array_push() expects parameter 1 to be array, object given in ...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,