php - Passing property to next method -


i have following code within class

class mail {     function addattachment($path, $name = '', $filetype = 'application/octet-stream') {         if (!@is_file($path)){             echo'<pre>filepath not found.</pre>';         }          if (empty($name)) {             echo 'no filename';         }          //store attachment in array         if(!isset($attachments)) {             $attachments = array();         }          $attachments[] = array('path' => $path,'name' => $name,'type' => $filetype);         //echo '<pre>';print_r($attachment);         return  $attachments;     }      function setmail() {         foreach ($this->$attachments $attachment) {             echo '<pre>';print_r($attachment);         }     } }  $mail = new mail; $mail->addattachment('../images/logo.png','filename'); $mail->addattachment('../images/logo.png','filensame'); $mail->setmail(); 

as can see, fristly create array's attachments (addattachment), works fine. though cannot seem use array in next method.

i try'd make $attachments property public, still these error messages:

(without public): cannot access empty property

(with public): cannot access empty property

(when using self::$attachments instead of $this::$attachments) :access undeclared static property:

can explain how can pass $attachments property setmail method?

thanks allready!

there no need send attachments setmail method. must done automatically. must declare attachments variable inside class. , when want access it, must $this->attachments:

<?php  class mail {     private $attachments = array();      function addattachment($path, $name = '', $filetype = 'application/octet-stream') {         $this->attachments[] = array('path' => $path,'name' => $name,'type' => $filetype);         return  $this->attachments;      }      function setmail()  {         foreach ($this->attachments $attachment) {             echo '<pre>';             print_r($attachment);             echo '</pre>';         }     } }  $mail = new mail; $mail->addattachment('../images/logo.png','filename1'); $mail->addattachment('../images/logo.png','filename2'); $mail->setmail();  ?> 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -