1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Proper code for calling my own function

Discussion in 'XenForo Development Discussions' started by AndyB, Oct 31, 2013.

  1. AndyB

    AndyB Well-Known Member

    In the following code example, my function is not returning the modified variable:

    PHP:
    <?php

    class Andy_ConvertImage_DataWriter extends XFCP_Andy_ConvertImage_DataWriter
    {
        public function 
    preSave()
        {
            
    // make sure preSave() is only run once
            
    if ($this->_preSaveCalled)
            {
                return;
            }
            
            
    // define variables
            
    $newMessage '';
            
    $myData '';

            
    $newMessage $this->get('message');
            
            
    $this->mainLoop($newMessage);
            
            
    $this->set('message'$myData);
            
            
    // call parent function
            
    parent::preSave();
        }
        
        public function 
    mainLoop($newMessage)
        {
            
    $myData $newMessage 'testing';
            return 
    $myData;
        }
    }

    ?>
    When I edit a post I should get the word "testing" added to the end of the message.

    At this point the $newMessage variable is being passed into my function, but the variable $myData is not being passed back. What am I doing wrong?

    Thank you.
     
    Last edited: Oct 31, 2013
  2. Jeremy

    Jeremy XenForo Moderator Staff Member

    You don't save the return of your function.
     
  3. AndyB

    AndyB Well-Known Member

    Hi Jeremy,

    Thank you for looking at the code. Where should I look to fix this:

    1) Add code to the mainLoop() function
    2) Add code to the preSave() function
     
  4. KenSmith

    KenSmith Active Member

    Probably this is what you meant:
    Code:
    $myData = $this->mainLoop($newMessage);
    
     
    AndyB likes this.
  5. AndyB

    AndyB Well-Known Member

    I sure appreciate the help, Ken. That worked perfect!

    PHP:
    <?php

    class Andy_ConvertImage_DataWriter extends XFCP_Andy_ConvertImage_DataWriter
    {
        public function 
    preSave()
        {
            
    // make sure preSave() is only run once
            
    if ($this->_preSaveCalled)
            {
                return;
            }
            
            
    // define variables
            
    $newMessage '';
            
    $myData '';

            
    $newMessage $this->get('message');
            
            
    $this->mainLoop($newMessage);
            
            
    $myData $this->mainLoop($newMessage);
            
            
    $this->set('message'$myData);
            
            
    // call parent function
            
    parent::preSave();
        }
        
        public function 
    mainLoop($newMessage)
        {
            
    $newMessage $newMessage 'testing';
            return 
    $newMessage;
        }
    }

    ?>
     
    Last edited: Oct 31, 2013
  6. Liam W

    Liam W Well-Known Member

    No point in doing something without learning ;)

    PHP:
    $myData $this->mainLoop($newMessage);
    That lines stores the value returned from the mainLoop function into the $myData variable.

    PHP:
    $this->mainLoop($newMessage);
    That would call the mainLoop function, and discard any returned value.
     
    AndyB likes this.
  7. euantor

    euantor Well-Known Member

    Just to add to this, you could pass the variable by reference to the method too to achieve a similar result.
     
  8. Liam W

    Liam W Well-Known Member

    True, but that would probably confuse them ;)
     
    Last edited: Oct 31, 2013
    euantor likes this.
  9. Jeremy

    Jeremy XenForo Moderator Staff Member

    Passing by reference is a perfectly valid practice, Liam. Run time pass by reference is the bad practice and PHP now warns and or prevents on it.
     
    Liam W likes this.
  10. euantor

    euantor Well-Known Member

    @Jeremy that's interesting to know. Was that introduced in 5.5 or 5.4? I tend only to concentrate on the bigger changes between versions when reading the announcements.
     
  11. Jeremy

    Jeremy XenForo Moderator Staff Member

  12. euantor

    euantor Well-Known Member

  13. Jeremy

    Jeremy XenForo Moderator Staff Member

    The method declaration is the proper use of a referenced variable, and it should have always been done there to make functions consistent between calls.
     
    euantor likes this.
  14. euantor

    euantor Well-Known Member

    Of course it is, most languages only allow it in the declaration. There are very few I know of that allow it at call time.
     
  15. AndyB

    AndyB Well-Known Member

    Thank you Liam,

    I really appreciate you taking the time to explain the above.

    So now I have the following code which works great:

    PHP:
    <?php

    class Andy_ConvertImage_DataWriter extends XFCP_Andy_ConvertImage_DataWriter
    {
        public function 
    preSave()
        {
            
    // make sure preSave() is only run once
            
    if ($this->_preSaveCalled)
            {
                return;
            }
            
            
    // define variables
            
    $newMessage '';
            
    $myData '';

            
    $newMessage $this->get('message');
            
            
    $myData $this->mainLoop($newMessage);
            
            
    $this->set('message'$myData);
            
            
    // call parent function
            
    parent::preSave();
        }
        
        public function 
    mainLoop($newMessage)
        {
            
    $newMessage $newMessage 'testing';
            return 
    $newMessage;
        }
    }

    ?>
     

Share This Page