2012-10-16 41 views
0

我對PHP有點新,並且對強類型語言如JAVA,C#或C++更有經驗。我目前正在使用PHP編寫Web工具,而我有一個問題想要做我想做的事情。在代碼中調用對象方法

我想在代碼中執行的簡單想法是通過一些使用PHP-IMAP獲取的電子郵件來運行的。然後我創建電子郵件對象(我定義的一個類),並將它們放入一個數組中。 但是,在代碼後面,我循環瀏覽這些電子郵件以顯示它們。 然而,正如你可能已經猜到我會遇到問題,我嘗試在後面的循環中使用電子郵件類對象方法 - 我很確定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; 
    } 
} 
?> 

的循環:

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

任何想法?

+1

你是不是做與返回任何有價值的東西。 'echo'。 – Jon

+0

不支持php4。取消'var'聲明並使用'public' /'private' /'protected'。此外,Theres沒有理由在這裏重新發明輪子,檢查'Zend_Mail'組件。 – prodigitalson

回答

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; 
+0

哦,我的天哪。我不能相信我忘了使用回聲 - 我很尷尬。 謝謝你有禮貌地指出我的完全noob錯誤。 :) –

+0

沒關係。成功之路充滿了錯誤:)祝你好運。 –