使用方法返回对象实例化变量

时间:2012-03-09 13:16:41

标签: php dom object methods

我一直在寻找几个小时,但我似乎无法理解为什么这不起作用。这是代码

我的目标是使用不同的方法加载XML文档,另一种方法是打印和管理该文档。

class ...

//Fetch and print xml document
    function fetchFromXMLDocument($XMLDocName) {
        $xmlDoc = new DOMDocument();
        $xmlDoc->load($XMLDocName);
        return $xmlDoc;

    }

在这里,我想将fetchFromXMLDocument()的值添加到我的$he变量中。    但它似乎没有工作?

function printXml($XMLDocName) {
   //this seems not to be right??       
    $he = fetchFromXMLDocument($XMLDocName);

    //after that this is what I want to do..
    // $items = $he->getElementsByTagName("item");
         ...
    }

有没有人知道为什么会这样?

3 个答案:

答案 0 :(得分:0)

$xmlDoc是一个DomDocument对象。你可能想要:

$he = $xmlDoc->saveXML();

答案 1 :(得分:0)

如果是在课堂上,你不必做吗

$he = $this->fetchFromXMLDocument($XMLDocName)

答案 2 :(得分:0)

问题是函数fetchFromXMLDocument();在类中。

如果您从printXML访问它的方法属于同一个类,那么您应该使用$this运算符访问它。

function printXml($XMLDocName) {    
     $he = $this -> fetchFromXMLDocument($XMLDocName);
     ...
}

然而,如果您从printXML访问它的方法在外面。然后首先你必须创建和对象的类并访问它。

function printXml($XMLDocName) {    
     $obj = new yourxmlclassname();
     $he = $obj -> fetchFromXMLDocument($XMLDocName);
     ...
}
相关问题