Calling Object Methods in Code

Posted by Mister R2 on Stack Overflow See other posts from Stack Overflow or by Mister R2
Published on 2012-10-16T22:44:13Z Indexed on 2012/10/16 23:00 UTC
Read the original article Hit count: 232

Filed under:
|
|
|

I'm a bit new to PHP, and I'm more experienced with strongly-typed languages such as JAVA, C# or C++.I'm currently writing a web tool in PHP, and I am having an issue trying to do what I want.

The simple idea of what I want to do in code is run through some emails I used PHP-IMAP to get. I then create email objects (a class I defined), and put them in an array. Later on the code, however, I cycle through those emails to display them. However, as you might have guessed I'd have an issue with, I try to use an Email Class object method in that later loop -- and I'm pretty sure PHP doesn't know that the variables in the array happen to be Email Class objects!

I wrote a toString method, and I want to call it in the loop. While I don't need to do this for the final version of this tool, I would like to find out what I'm missing.

This is the class and the loop where I'm calling the method:

 include 'imap_email_interface.php';


 class ImapEmail implements imap_email_interface
 {
    // Email data
    var $msgno;
    var $to;
    var $from;
    var $subject;
    var $body;
    var $attachment;

    // Email behavior
    /* PHP 4 ~ legacy constructor */
    public function ImapEmail($message_number)
    {
        $this->__construct();
        $this->msgno = $message_number;
    }

    /* PHP 5 Constructor */
    public function __construct($message_number)    
    {   $this->msgno = $message_number; }

    public function send($send_to)
    {
        // Not Yet Needed! Seriously!
    }

    public function setHeaderDirectly($TO, $FROM, $SUBJECT)
    {   $this->to = $TO;    $this->from = $FROM;    $this->subject = $SUBJECT;  }

    public function setHeaderIndirectly($HEADER)
    {
        if (isset($HEADER->to[0]->personal))
            $this->to = '"'.$HEADER->to[0]->personal.'", '.$HEADER->to[0]->mailbox.'@'.$HEADER->to[0]->host;
        else
            $this->to = $HEADER->to[0]->mailbox.'@'.$HEADER->to[0]->host;
        $this->from = '"'.$HEADER->from[0]->personal.'", '.$HEADER->from[0]->mailbox.'@'.$HEADER->from[0]->host;
        $this->subject = $HEADER->subject;
    }

    public function setBody($BODY)
    {   $this->body = $BODY;    }

    public function setAttachment($ATTCH)
    {
        $this->attachment = $ATTCH;
    }

     public function toString()
     {
        $str = '[TO]: ' . $this->to . '<br />' . '[FROM]: ' . $this->from . '<br />' . '[SUBJECT]: ' . $this->subject . '<br />';
        $str .= '[Attachment]: '.$this->attachment.'<br />';

        return $str;
     }
 }
?>

The Loop:

foreach ($orderFileEmails as $x)
    {
        $x->toString();
        echo '<br /><br />';
    }

Any ideas?

© Stack Overflow or respective owner

Related posts about php

Related posts about object