• 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

AndyB

Well-known member
#1
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:

AndyB

Well-known member
#5
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:

Liam W

Well-known member
#6
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.
 

euantor

Well-known member
#7
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.
 

Jeremy

Well-known member
#9
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.
 

euantor

Well-known member
#10
@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.
 

Jeremy

Well-known member
#13
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.
 

AndyB

Well-known member
#15
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;
	}
}

?>