0

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?

2
  • 1
    You are not doing anything with the return value. echo. Commented Oct 16, 2012 at 22:46
  • Dont support php4. Kill the var declarations and use public/private/protected instead. Additionally Theres no reason to reinvent the wheel here, checkout the Zend_Mail component. Commented Oct 16, 2012 at 22:50

1 Answer 1

2

As written in your code, your toString function returns the $str variable which represent the content of the email.

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

      return $str; //You RETURN the string
 }

In your loop , you are just calling the function and do "nothing" with the returned value. Use echo to print it returned value to the screen.

foreach ($orderFileEmails as $x)
    {
        echo $x->toString(); //Notice the echo I've added
        echo '<br /><br />';
    }

Another thing is , make sure that $orderFileEmails is an array of objects, otherwise this loop won't do what you're willing it to do. In order to debug it, before the loop write: var_dump($orderFileEmails);.

EDIT: Another option is to use magic method __toString(). (Manual - http://www.php.net/manual/en/language.oop5.magic.php#object.tostring)

It works like that:

class ImapEmail
{
    // Your methods and properties here...

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

      return $str;
    }
}

$class = new ImapEmail();
echo $class;
Sign up to request clarification or add additional context in comments.

2 Comments

Oh my gosh. I can't believe I forgot to use echo -- I'm embarrassed. Thank you for being polite about pointing out my completely noob mistake. :)
That's ok. The way to success is full of mistakes :) Good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.