What does -> do?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Manfred Kooistra

    #1

    What does -> do?

    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
    else)?

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

  • raghuanandy@gmail.com

    #2
    Re: What does -> do?


    Manfred Kooistra wrote:
    I have these pieces of example code that I am trying to figure out:
    >
    (a) $a = $b->c();
    (b) $d->e();
    >
    What does "->" do?
    Basically, "->" use to reference variables or functions (or more
    precisely members and methods) within a class.
    eg:
    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 "->"
    operator
    $obj = new Something('Worl d');

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

    //accessing member "a" of object "obj" using ->
    $obj->a = 'Hello';
    print $obj->a;
    Where is there a reference/explanation in the PHP manual (or anywhere
    else)?
    >
    (Unfortunately Google et al. don't search for these characters, so I
    don't know how to look for an explanation on this.)
    For more information visit:
    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.

    &


    Comment

    • Manfred Kooistra

      #3
      Re: What does -> do?

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

      So why does this work:

      require('Class_ definition.php' );
      $object = new Class;
      $object->function_defin ed_in_class($so mething);
      other_function_ defined_in_clas s($object);

      but not this:

      require('Class_ definition.php' );
      $object = new Class;
      while($somethin g) {
      $object->function_defin ed_in_class($so mething);
      }
      other_function_ defined_in_clas s($object);

      ?

      Comment

      • Rik

        #4
        Re: What does -> do?

        Manfred Kooistra wrote:
        Okay, understood: classes and objects
        (http://de.php.net/manual/en/language.oop.php).
        >
        So why does this work:
        >
        require('Class_ definition.php' );
        $object = new Class;
        $object->function_defin ed_in_class($so mething);
        other_function_ defined_in_clas s($object);
        >
        but not this:
        >
        require('Class_ definition.php' );
        $object = new Class;
        while($somethin g) {
        $object->function_defin ed_in_class($so mething);
        }
        other_function_ defined_in_clas s($object);
        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_define d_in_class, else it'd be an infinite loop....
        --
        Rik Wasmus


        Comment

        • Manfred Kooistra

          #5
          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",
          $_FILES['upfile_0']['name']);

          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'] .
          $uploaded_file_ name;

          if(is_file($upl oaded_file_path )){
          preg_match('/(\.\w+)$/', $uploaded_file_ name, $match);
          $typ = substr($match[1], 1);

          $handle = fopen($uploaded _file_path, 'r');
          $file = fread($handle, filesize($uploa ded_file_path)) ;
          $mime->addAttachment( $file, "image/$typ", $uploaded_file_ name);
          fclose($handle) ;

          $i++;
          }
          }
          }

          $_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->addAttachmen t 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 .= $sometextfragme nt; works fine.

          Comment

          • Rik

            #6
            Re: What does -> do?

            Manfred Kooistra wrote:
            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",
            $_FILES['upfile_0']['name']);
            >
            Now, with an unknown number of files, I changed that part of the code
            to (exactly as in my script):
            >
            <SNIP>
            $_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->addAttachmen t 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 .= $sometextfragme nt; works fine.

            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($fi lename,$fallbac k){
            if(function_exi sts('finfo_file ')){
            $finfo = finfo_open(FILE INFO_MIME);
            return finfo_file($fin fo, $filename);
            }
            $mime_magic = mime_content_ty pe($filename);
            return ($mime_magic==f alse) ? $fallback : $mime_magic;
            }
            foreach($_FILES as $file){
            if($file['error'] == UPLOAD_ERR_OK){
            $mime->addAttachment( $file['tmp_name'],
            return_mime($fi le['name'],$file['type']),$file['tmp_name']);
            } 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


            Comment

            • Manfred Kooistra

              #7
              Re: What does -&gt; 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($uploa ded_file_path)) ;
              $mime->addAttachment( $file, "image/$typ", $uploaded_file_ name);
              fclose($handle) ;

              Working:

              $mime->addAttachment( $uploaded_file_ path, "image/$typ",
              $uploaded_file_ name);

              (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!

              Comment

              • Rik

                #8
                Re: What does -&gt; do?

                Manfred Kooistra wrote:
                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($uploa ded_file_path)) ;
                $mime->addAttachment( $file, "image/$typ", $uploaded_file_ name);
                fclose($handle) ;
                >
                Working:
                >
                $mime->addAttachment( $uploaded_file_ path, "image/$typ",
                $uploaded_file_ name);
                >
                (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.
                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


                Comment

                Working...