What does -> do?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have these pieces of example code that I am trying to figure out:

(a) $a = $b->c();
(b) $d->e();

What does "->" do?
Where is there a reference/explanation in the PHP manual (or anywhere

(Unfortunately Google et al. don't search for these characters, so I
don't know how to look for an explanation on this.)

Re: What does -> do?

Manfred Kooistra wrote:
Quoted text here. Click to load it
    Basically, "->" use to reference variables or functions (or more
precisely members and methods) within a class.
class Something {
  public $a = 'Hello';
  function __construct($a)
  {   $this->a = $a; }
  function SetA($a);
  { $this->a = $a; }
  function GetA()
  { return $this->a; }
} //end class

Now the member "a" and methods SetA , GetA can be accessed via the "->"
$obj = new Something('World');

//Access method "GetA" of object "obj" using ->
print $obj->GetA();

//accessing member "a" of object "obj" using ->
$obj->a = 'Hello';
print $obj->a;

Quoted text here. Click to load it

For more information visit:

Re: What does -> do?

Okay, understood: classes and objects
(http://de.php.net/manual/en/language.oop.php ).

So why does this work:

$object = new Class;

but not this:

$object = new Class;
while($something) {


Re: What does -> do?

Manfred Kooistra wrote:
Quoted text here. Click to load it

It highly depends on the specific class offcourse. Normally it would work,
unless there's something else going on inside the class. Offcourse, it
would only work when $something is altered by the
function_defined_in_class, else it'd be an infinite loop....
Rik Wasmus

Re: What does -> do?

Thank you so far.

To make this more concrete:
I am trying to send a variable number of attachments with PEAR::Mail.

With a fixed number of attachments (example with 1 file), the relevant
part of the code was this:

$mime->addAttachment($file, "image/$type_of_file",

Now, with an unknown number of files, I changed that part of the code
to (exactly as in my script):

$i = 0;
foreach($_POST_DATA as $key => $value){
    if(preg_match("/^upfile_/i", $key)){
        $uploaded_file_name = $value;
        $uploaded_file_path = $_POST_DATA['upload_dir'] .

            preg_match('/(\.\w+)$/', $uploaded_file_name, $match);
            $typ = substr($match[1], 1);

            $handle = fopen($uploaded_file_path, 'r');
            $file = fread($handle, filesize($uploaded_file_path));
            $mime->addAttachment($file, "image/$typ", $uploaded_file_name);


$_POST_DATA is stuff processed through a Perl script. Files are named
upfile_0, upfile_1 etc. The data is all there, I checked it. I can even
output $file to the screen, but $mime->addAttachment seems not do do
anything, because the emails arrive without attachment (all other
content is okay, since the rest of the [rather very long, so not posted
here] script is unchanged).

It seems to me that somehow the foreach and the -> don't work well
together, because I have the same foreach loop for the email content,
where $text .= $sometextfragment; works fine.

Re: What does -> do?

Manfred Kooistra wrote:
Quoted text here. Click to load it

There is absolutely no problem in using objects within a loop. It's the
logic that is faulty. I do not use the PEAR::Mail class, but I suspect this
will solve your problem (untested, might be one or two typos):

function return_mime($filename,$fallback){
        $finfo = finfo_open(FILEINFO_MIME);
        return finfo_file($finfo, $filename);
    $mime_magic = mime_content_type($filename);
    return ($mime_magic==false) ? $fallback : $mime_magic;
foreach($_FILES as $file){
    if($file['error'] == UPLOAD_ERR_OK){
    } else {
        trigger_error("{$file['name'] is not uploaded correctly.");

I do not know how your PERL script works, or what it does, so I have not
used that. You should be able te get it to work using this example though.
Rik Wasmus

Re: What does -> do?

Thank you, Rik.
Alas, that wasn't it.

I found out that the problem was trying to attach the file after
fopening and freading it. On debug, I got an error message telling me:
"File is not readable." I was wondering about that, because that files
permissions and path are alright. So I simply attached it without
fopening it - and that did the trick!

Not working:

$handle = fopen($uploaded_file_path, 'r');
$file = fread($handle, filesize($uploaded_file_path));
$mime->addAttachment($file, "image/$typ", $uploaded_file_name);


$mime->addAttachment($uploaded_file_path, "image/$typ",

(with $typ preg_match/substr-ed from the file name)

I wonder about this, because I found hundreds of
mail-attachment-examples with fopen/fread, but I'm happy that my script
is running and I get mailed the uploaded files.

Thank you to all of you!

Re: What does -> do?

Manfred Kooistra wrote:
Quoted text here. Click to load it

Well, it always pays to read up on the functions. Does it take the data of
a file, or the filename, that's the only difference.
Rik Wasmus

Site Timeline