Results 1 to 4 of 4

Thread: How to call one class function from another class function?

  1. #1
    Experienced user
    Join Date
    06-20-09.
    Posts
    1,311

    Default 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

  2. #2
    User
    Join Date
    06-19-09.
    Location
    Kosice, Slovakia
    Posts
    61

    Default Re: How to call one class function from another class function?

    Code:
    $writer = new WriteTheXML();
    $writer->writeXMLFile($parentid);
    S.C.A.R.E

  3. #3
    Experienced user
    Join Date
    06-20-09.
    Posts
    1,311

    Default 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

  4. #4
    Experienced user
    Join Date
    06-10-09.
    Location
    Cape Town, South Africa
    Posts
    1,387

    Default 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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •