Can I nest a function within the controller?

Permalink
I've got a block whose view.php contains a form. Action of the form is a function within controller.php. This function contains a switch() and there is a good chunk of code that is the same for each case within the switch. So, I thought it'd be handy to just put all of that code in a function and call it in each case. Can I create a function within my public function?

This is my public function which is the action of my view.php's form:
public function action_create_reminder() {
...bunch of stuff here ....
}


This may be some silly PHP rule I'm not following...IDK. I've not messed with functions and nesting (whether you should or shouldn't etc).

I was think of something like:

public function action_create_reminder() {
function new_function() {
....code that is the same for every case here ...
}
...rest of the code that isn't the same for every case here ...
}


Thoughts?

tash
 
olliephillips replied on at Permalink Best Answer Reply
olliephillips
What you need to do is create private method, which is basically a function in the context of a class - private means it can only be called from your class/controller.

You would do this outside of your action_create_reminder function:-
private function new_function($arguments){
## The code you wish to run 
## The value you want to return to your main function
return $someValue;
}


You call the above using this code, placed within your switch statement

##
##
$this->new_function($arguments);
##
##


Hope that helps
tash replied on at Permalink Reply
tash
Thanks :-)