Proper code for calling my own function

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:
Probably this is what you meant:
Code:
$myData = $this->mainLoop($newMessage);

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:
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.
 
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.

Just to add to this, you could pass the variable by reference to the method too to achieve a similar result.
 
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.
 
@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.
 
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.
 
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.
 
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.

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;
	}
}

?>
 
Back
Top Bottom