Crear una instancia de una variable con un objeto de retorno de métodos

I have been searching for hours but I can't seem to understand why this does not work. Here is the code

My goal is to have a different method to load the XML document, and another one to print and manage that document.

class ...

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


Here I want do add the value of the fetchFromXMLDocument() a mi $he variable. but it does not seem to be working?

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");

Does anybody have an idea on why that might be?

preguntado el 09 de marzo de 12 a las 13:03

Can you explain what does not seem to be working? -

@jprofitt It does not seem to instiate that object xmlDoc to my variable $he. Maybe this makes no sense in PHP, maybe I'm wrong with this seems I have done this on java?? -

¿Qué var_dump($he) ¿darte? -

3 Respuestas

El problema es esa función fetchFromXMLDocument(); is inside a class.

If the method you are accessing it from printXML, is within the same class then you should access it using $this operador.

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

Sin embargo, If the method you are accessing it from printXML, is outside. Then first you have to create and object of the class and access it.

function printXml($XMLDocName) {    
     $obj = new yourxmlclassname();
     $he = $obj -> fetchFromXMLDocument($XMLDocName);

Respondido el 20 de junio de 20 a las 10:06

$xmlDoc is a DomDocument object. You probably want:

$he = $xmlDoc->saveXML();

respondido 09 mar '12, 13:03

pero $he is in a different method that $xmlDoc What I need is to use the content of the $xmlDoc in another method than the one that it is in? - Eirc hombre

Oh, I want to still use it as a DomDocument object, not get the XML output of it. returning like you are should give that to you. Are you sure that the value being returned is a valid object? (that the file was imported successfully?) - enygma

hey thank you for you feedback. The problem was that I did not use $this since I was doing that inside a class.. - Eirc hombre

if it's in a class, wouldn't you have to do

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

respondido 09 mar '12, 13:03

try it. You never said what about it wasn't working, so that really didn't give us much to work with. It would be helpful if you add error reporting and let us know what the errors are. - El científico

Thank you so much, that was the problem :) - Eirc hombre

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.