php classes calling functions using :: and ->

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anthony Smith

    php classes calling functions using :: and ->

    I can call a class using "->", but it complains about the ::
    I see on the net where :: is used. Is there a good explanation on when
    to use one over the other or the differences?

    $help = new help();
    $help->foo();
    $help::foo();


    class help{

    function foo(){
    echo "test";
    }
    }

  • peter

    #2
    Re: php classes calling functions using :: and ->

    >I can call a class using "->", but it complains about the ::
    I see on the net where :: is used. Is there a good explanation on when
    to use one over the other or the differences?
    >
    $help = new help();
    $help->foo();
    $help::foo();
    >
    >
    class help{
    >
    function foo(){
    echo "test";
    }
    }
    >
    :: is used to call class methods or properties when the class has not been
    initiated:-

    PHP is a popular general-purpose scripting language that powers everything from your blog to the most popular websites in the world.



    Comment

    • Rami Elomaa

      #3
      Re: php classes calling functions using :: and ->

      Anthony Smith kirjoitti:
      I can call a class using "->", but it complains about the ::
      I see on the net where :: is used. Is there a good explanation on when
      to use one over the other or the differences?
      >
      $help = new help();
      $help->foo();
      $help::foo();
      >
      >
      class help{
      >
      function foo(){
      echo "test";
      }
      }
      >
      :: is used for calling a method by class, not object. This is the part
      where php becomes rocket science. You can call a method of a class
      without having created an instance of the class eq. an object by calling
      it via the class.

      So if you have

      class help {
      function foo(){
      echo "Hello kitty!";
      }
      }

      You can call the method foo without creating an object:
      help::foo();

      Or you can create an instance of the class:
      $myhelp = new help();

      and then use the class pointer to call the method
      $myhelp->foo();

      Why use :: instead of -then? hmm... I dunno, it's kewl? I've only used
      it once with a singleton where it seemed to make sense buy it's actually
      quite rare I think. People who work with objects big-time may have use
      for it, but I don't think it's actually all that necessary. It's good to
      have and good to know what it is and what it does, but on most days
      you'll be fine without it.

      --
      Rami.Elomaa@gma il.com
      "Olemme apinoiden planeetalla."

      Comment

      • peter

        #4
        Re: php classes calling functions using :: and ->

        Why use :: instead of -then? hmm... I dunno, it's kewl? I've only used
        it once with a singleton where it seemed to make sense buy it's actually
        quite rare I think. People who work with objects big-time may have use for
        it, but I don't think it's actually all that necessary. It's good to have
        and good to know what it is and what it does, but on most days you'll be
        fine without it.
        I think it is done to lower the overheads if for example you only need to
        use 1 function within the class. Instead of having the overhead of
        instantiating the class then calling the method (which I presume allocates
        memory for the methods and properties).


        Comment

        • ZeldorBlat

          #5
          Re: php classes calling functions using :: and ->

          On Mar 23, 11:44 am, Rami Elomaa <rami.elo...@gm ail.comwrote:
          Anthony Smith kirjoitti:
          >
          >
          >
          I can call a class using "->", but it complains about the ::
          I see on the net where :: is used. Is there a good explanation on when
          to use one over the other or the differences?
          >
          $help = new help();
          $help->foo();
          $help::foo();
          >
          class help{
          >
          function foo(){
          echo "test";
          }
          }
          >
          :: is used for calling a method by class, not object. This is the part
          where php becomes rocket science. You can call a method of a class
          without having created an instance of the class eq. an object by calling
          it via the class.
          >
          So if you have
          >
          class help {
          function foo(){
          echo "Hello kitty!";
          }
          >
          }
          >
          You can call the method foo without creating an object:
          help::foo();
          >
          Or you can create an instance of the class:
          $myhelp = new help();
          >
          and then use the class pointer to call the method
          $myhelp->foo();
          >
          Why use :: instead of -then? hmm... I dunno, it's kewl? I've only used
          it once with a singleton where it seemed to make sense buy it's actually
          quite rare I think. People who work with objects big-time may have use
          for it, but I don't think it's actually all that necessary. It's good to
          have and good to know what it is and what it does, but on most days
          you'll be fine without it.
          >
          --
          Rami.Elo...@gma il.com
          "Olemme apinoiden planeetalla."

          It's really not all that rare -- in fact it's quite common.

          As a matter of form, you should define class methods as static and
          call them with :: If you get into the habit of calling your instance
          methods with :: you can run into problems because $this is not
          available (and PHP will complain accordingly). Furthermore there are
          differences in inheritance between static methods and instance
          methods -- namely that there is no inheritance with static methods --
          but that provides you with a certain performance benefit.

          The short answer (and it's certainly not the right one in all cases)
          is that if you use $this in your method, make it an instance method
          and call it with ->. If you don't use $this in your method, declare
          it static and call it with ::

          Comment

          • Henk verhoeven

            #6
            Re: php classes calling functions using :: and -&gt;

            peter schreef:
            >Why use :: instead of -then? hmm... I dunno, it's kewl? I've only used
            >it once with a singleton where it seemed to make sense buy it's actually
            >quite rare I think. People who work with objects big-time may have use for
            >it, but I don't think it's actually all that necessary. It's good to have
            >and good to know what it is and what it does, but on most days you'll be
            >fine without it.
            >
            I think it is done to lower the overheads if for example you only need to
            use 1 function within the class. Instead of having the overhead of
            instantiating the class then calling the method (which I presume allocates
            memory for the methods and properties).
            imho it would also be bad design / sort of misleading to create an
            object you only use for calling a function on that does not need any
            information from the object itself. With php5 you can also declare the
            function static, so that when the code is maintained (maybe years later
            by another programmer) it won't get modified to expect the object to be
            properly initialized.

            To say it an other way, making a static function and using :: will
            communicate your intentions more clearly in your source code.

            (i don't think the methods need extra memory for each extra object you
            instantiate. And php only adds properties that are either declared or
            actually assigned. But the object itself will probably be allocated, and
            deallocated when it is no longer referenced, so i think you are right
            about the overhead)

            Greetings,

            Henk Verhoeven,
            www.phpPeanuts.org.

            Comment

            • Toby A Inkster

              #7
              Re: php classes calling functions using :: and -&gt;

              Anthony Smith wrote:
              I can call a class using "->", but it complains about the ::
              The difference between them is much the same as the difference between an
              object and a class.

              class Person
              {
              public $firstname;
              public $surname;
              public $spouse;

              public function __construct ($firstname, $surname)
              {
              $this->firstname = $firstname;
              $this->surname = $surname;

              echo $this->get_name()." was born.\n";
              }

              public function get_name ()
              {
              return sprintf("%s %s",
              $this->firstname,
              $this->surname);
              }

              public function introduce_mysel f ()
              {
              return "My name is ".$this->get_name()."\n ";
              }

              public static function wedding ($a, $b, $changename=FAL SE)
              {
              if (!empty($a->spouse) || !empty($b->spouse))
              return FALSE;

              $a->spouse = $b;
              $b->spouse = $a;

              printf("%s and %s are married!\n",
              $a->get_name(),
              $b->get_name());

              if ($changename)
              $b->surname = $a->surname;

              return TRUE;
              }
              }

              $d = new Person('Dave', 'Jones');
              $m = new Person('Mary', 'Smith');
              $d->introduce_myse lf();
              $m->introduce_myse lf();
              Person::wedding ($d, $m, TRUE);
              $m->introduce_myse lf();

              The -operator operates on objects, the :: operator operates on classes.

              --
              Toby A Inkster BSc (Hons) ARCS
              Contact Me ~ http://tobyinkster.co.uk/contact
              Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

              * = I'm getting there!

              Comment

              Working...