Best way to call an object method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ronnil
    Recognized Expert New Member
    • Jun 2007
    • 134

    Best way to call an object method

    Hi

    I'm sitting here making a method in a object. The method doesn't really require instatiation of the object, and then i started wondering.... is this correct, and what is the best way to call this method?

    meaning:
    This method could actually be a global function (what it does can be used in other conjuctions) but I'm not really planning to use the function in other context than the specific object.

    Now, when i have to call it in my code. Which is faster? calling it from the instantiated object? like
    [CODE=php]
    <?php
    $object->method($parame ter);
    ?>
    [/CODE]

    or should i treat it in a more abstract manner like

    [CODE=php]
    <?php
    $object::method ($paremeter);
    ?>
    [/CODE]

    Personally I'd say use the instantiated object, but is it a matter of coding style or is there a really good reason why to prefer one over the other? (as I'm sure there is)
  • ronnil
    Recognized Expert New Member
    • Jun 2007
    • 134

    #2
    ooops posted in articles section, please move it to questions :S

    Comment

    • ak1dnar
      Recognized Expert Top Contributor
      • Jan 2007
      • 1584

      #3
      Originally posted by ronnil
      ooops posted in articles section, please move it to questions :S
      You have posted this in the ....... :D

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Ronnil.

        If you don't need to instantiate the object to invoke the method, then it is best to make it static, since that's the whole point of the static keyword.

        The main benefit of using static methods is that you're not invoking all that overhead by needlessly instantiating the object.

        Additionally, by 'attaching' the method to a particular class rather than making it a procedure, you allow for the possibility of having two methods with the same name (but different classes).

        Comment

        • ronnil
          Recognized Expert New Member
          • Jun 2007
          • 134

          #5
          I'm with you that far pbmods :)

          In this particular case I have in my mind, the object is in fact instantiated, so when this is the case, is there a speed/memory increase to gain using one method over the other?

          take this example

          [CODE=php]
          <?php
          class MyClass
          {
          function my_static_funct ion()
          {
          echo "This function is static";
          }
          }

          $object = new MyClass();

          //Now is there any perfomance increase whichever of these two calls i make?

          MyClass::my_sta tic_function();

          $object->my_static_func tion();

          ?>
          [/CODE]

          btw, I'm sad to say that my hosting provider still uses php 4, so don't have all the nice object features....jus t the basic ones...

          Comment

          • pbmods
            Recognized Expert Expert
            • Apr 2007
            • 5821

            #6
            Heya, Ronnil.

            If you call it statically, I would guess that PHP wouldn't bother setting up $this, but that's about the most I can think of that you would gain, performance-wise.

            Comment

            Working...