Page 1 of 1
calling a method within the scope of another method
Posted: Fri Feb 05, 2010 7:14 pm
by cone13cone
I'm going to use a simple example hopefully I can get my point across. I need the wrap_this method to be able to be called inside the scope of each method and return the method magic const, and as well have access to that methods func_get_args(); Im not really sure on where to start looking to even find out if this is possible.
Code: Select all
class = Invoice {
public function get_hours(){
if(something) {
$this->wrap_this();
}
return $hours;
}
public function get_materials(){
if(something) {
$this->wrap_this();
}
return $materials;
}
protected function wrap_this(){
$this->_args = func_get_args();
$this->_method = __METHOD__;
}
}
Re: calling a method within the scope of another method
Posted: Fri Feb 05, 2010 7:34 pm
by AbraCadaver
This is not clear at all. What do you expect $args amd __METHOD__ to be in the code you posted. I'm sure that from a high level there is a better way to do what you are trying to do.
Re: calling a method within the scope of another method
Posted: Fri Feb 05, 2010 7:45 pm
by cone13cone
AbraCadaver wrote:This is not clear at all. What do you expect $args amd __METHOD__ to be in the code you posted. I'm sure that from a high level there is a better way to do what you are trying to do.
when wrap_this is called in get_hours i would like for it to return
$args = the arguments passed to get_hours
and I would want method to return Invoices::get_hours;
when wrap_this is called in get_materials i would like for it to return
$args = the arguments passed to get_materials
and I would want method to return Invoices::get_materials;
I hope this clears things up, and as for there being a better way, there could be a better option for me but I have not come up with it yet.
Re: calling a method within the scope of another method
Posted: Fri Feb 05, 2010 7:57 pm
by AbraCadaver
As for your example, there is no way to do this. Function variables are local to that function. I think you may have a fundamental design flaw.
Re: calling a method within the scope of another method
Posted: Fri Feb 05, 2010 8:32 pm
by cone13cone
AbraCadaver wrote:As for your example, there is no way to do this. Function variables are local to that function. I think you may have a fundamental design flaw.
There is a way: its to pass __METHOD__ and func_get_args() as arguments to the wrapper method. Was just trying to find the easiest way.
Re: calling a method within the scope of another method
Posted: Fri Feb 05, 2010 8:39 pm
by AbraCadaver
cone13cone wrote:AbraCadaver wrote:As for your example, there is no way to do this. Function variables are local to that function. I think you may have a fundamental design flaw.
There is a way: its to pass __METHOD__ and func_get_args() as arguments to the wrapper method. Was just trying to find the easiest way.
Uhh, yeah... But you said you wanted wrap_this() to return those. I guess I thought that it would be really stupid to pass vars to a function just so that function could return the vars straight back. If all you want to do is set the vars to a class var, then why not just set them in the respective functions?
Without knowing what you're trying to accomplish from a high level, advice is limited.
Re: calling a method within the scope of another method
Posted: Fri Feb 05, 2010 9:24 pm
by cone13cone
AbraCadaver wrote:cone13cone wrote:AbraCadaver wrote:As for your example, there is no way to do this. Function variables are local to that function. I think you may have a fundamental design flaw.
There is a way: its to pass __METHOD__ and func_get_args() as arguments to the wrapper method. Was just trying to find the easiest way.
Uhh, yeah... But you said you wanted wrap_this() to return those. I guess I thought that it would be really stupid to pass vars to a function just so that function could return the vars straight back. If all you want to do is set the vars to a class var, then why not just set them in the respective functions?
Without knowing what you're trying to accomplish from a high level, advice is limited.
Sorry if you misunderstood my goal was not to pass them right back, but to wrap the result as shown below.
My ultimate goal is to wrap what is returned from my db with handles to that class and method:
So in this example get_invoices would return
<div class = 'eup_invoice' data-class='Invoice' data-method='get_hours' data-id='1029'> value returned from get_hours</div>
when I need to update the text node without refresh, i can net the data-(HTML 5 I know) attribute and pass them to an ajax updater function which would call something like below.
$updatedNode = new $_GET['class'];
$updatedNode -> $_GET['method']();
The problem is I have about 50 methods that I need to "map" and wrap around their own value, I could always just wrap them inside each method, but I believe their has to be a better way to do it, would seem like an immense amount of repetition.
Hope this clears things up, and I am open to any other options or ideas.
Re: calling a method within the scope of another method
Posted: Fri Feb 05, 2010 9:40 pm
by AbraCadaver
Flip your thinking around. Send all data to 1 function (a router or controller function) and have it parse the stuff you send and then call the appropriate function based on the arguments.
Re: calling a method within the scope of another method
Posted: Fri Feb 05, 2010 10:00 pm
by AbraCadaver
Sorry I mis-interpreted your last post. You'll either have to do it the way you have stated (pass the args to the wrapper) or have a caller function somewhere that takes the class, method and args, builds the div and calls the proper method to get the DB results.
Re: calling a method within the scope of another method
Posted: Fri Feb 05, 2010 10:12 pm
by cone13cone
AbraCadaver wrote:Sorry I mis-interpreted your last post. You'll either have to do it the way you have stated (pass the args to the wrapper) or have a caller function somewhere that takes the class, method and args, builds the div and calls the proper method to get the DB results.
Yeah I'm going to play around with it some more, I like the idea of a caller function though.
Thanks for your help.