在代码中调用对象方法

时间:2012-10-16 22:44:14

标签: php object types casting

我对PHP有点新手,而且我对使用强类型语言(如JAVA,C#或C ++)更有经验。我目前正在用PHP编写一个Web工具,我遇到了一个问题。做我想做的事。

我想在代码中做什么的简单想法是通过我使用PHP-IMAP获取的一些电子邮件来运行的。然后我创建了电子邮件对象(我定义的类),并将它们放在一个数组中。 然而,稍后在代码中,我会循环显示这些电子邮件以显示它们。 但是,正如您可能已经猜到我遇到了问题,我尝试在后面的循环中使用Email Class对象方法 - 我很确定PHP不知道数组中的变量恰好是电子邮件类对象!

我写了一个toString方法,我想在循环中调用它。 虽然我不需要为此工具的最终版本执行此操作,但我想了解我所缺少的内容。

这是我调用方法的类和循环:

 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 />';
    }

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

如您的代码所示,您的toString函数会返回代表电子邮件内容的$str变量。

 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
 }

在你的循环中,你只是调用函数并对返回的值做“无”。 使用echo将返回值打印到屏幕上。

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

另一件事是,确保$orderFileEmails是一个对象数组, 否则这个循环不会做你愿意做的事情。 为了调试它,在循环写入之前:var_dump($orderFileEmails);

编辑:另一种选择是使用magic method __toString()。 (手册 - http://www.php.net/manual/en/language.oop5.magic.php#object.tostring

它的工作原理如下:

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;