How to call one class function from another class function?
I have two classes in a module (WriteTheXML.php and ViewFeed.php), and I wish to invoke a function in WriteTheXML from ViewFeed (the function being called is writeXMLFile())
I'm can't find the right syntax to use, these are a few I've tried and the errors I get.
Code:
writeXMLFile($parentid);
//Call to undefined function writeXMLFile($parentid);
$this->writeXMLFile($parentid);
//Fatal error: Call to undefined method ViewFeed::writeXMLFile() .....
WriteTheXML:writeXMLFile($parentid);
//Non-static method WriteTheXML::writeXMLFile() should not be called statically, assuming $this from incompatible context .....
$this->WriteTheXML->writeXMLFile($parentid);
//Fatal error: Call to a member function writeXMLFile() on a non-object ......
$anobject = WriteTheXML::writeXMLFile($parentid);//Non-static method WriteTheXML::writeXMLFile() should not be called statically, assuming $this from incompatible context
$anobject = WriteTheXML->writeXMLFile($parentid);
//Parse error: syntax error, unexpected T_OBJECT_OPERATOR
OOP DUMMY ... Clues appreciated :)
Re: How to call one class function from another class function?
Code:
$writer = new WriteTheXML();
$writer->writeXMLFile($parentid);
Re: How to call one class function from another class function?
Thank you s.c.a.r.e,
Works perfectly, and that actually clues me in heaps re coding (i'm looking at pages of scripts with a new vision).
:D
Re: How to call one class function from another class function?
If your 2nd class doesn't require any other object instances or object variables and has been coded seperately, then you can call its member function directly.
Code:
WriteTheXML::writeXMLFile($parentid);
If you look at leo's Calendar module, he calls a function from the Calendar.php object directly in the Events.php object. This is great for functions to be shared, but not to be part of constructing an entire object just to call 1 function.