March 9, 2012
Instantiate an variable with a methods return object
Question by Eirc man
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();
$xmlDoc->load($XMLDocName);
return $xmlDoc;
}
Here I want do add the value of the fetchFromXMLDocument()
to my $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?
Answer by Starx
The problem is that function 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
operator.
function printXml($XMLDocName) {
$he = $this -> fetchFromXMLDocument($XMLDocName);
...
}
However, 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);
...
}