Reputation: 881
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?
Upvotes: 0
Views: 89
Reputation: 10346
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;
Upvotes: 2