How to pass function with parameters to another function in PHP?
Hello friends,
I am last year MCA student. I recently started learning Python language. In one of my program I have to write code like following:
Code:
def lites(x,y,z):
// some code
def bigs(funcs): # funcs = callables()
// some code
#main
bigs(lites(1,2,3))
I don't know how to do this. Can anyone tell me how to pass function with parameters to another function in PHP? Please help me.
Thank you.
Re: How to pass function with parameters to another function in PHP?
Hey it is very easy process to pass function with parameters to another function in PHP. To do this you have to use array in your code. You have to first declare one function in following ways.
Code:
bigs(lites, (1, 2, 3))
After this you have to use variable of array like "args" to store first function in it like following ways.
Code:
def bigs(funcs, args):
funcs(*args)
Re: How to pass function with parameters to another function in PHP?
This type of problem can be solve using following two ways.
1.You can solve such type of problem by using lambda, but there is one requirement of it. You have to use passed function with one argument. It means you have to change big().
2.You have to use named function. This function called the original functions with arguments. Remember that such type of function declared inside other function.
Re: How to pass function with parameters to another function in PHP?
I have written following code for you. In the following code I have passed one function with parameters to another function. It is very easy to understand. In the following code I have use Param and lambda keyword to do this. First I have cretae one function known as bigs1(func) and after that I have create another function like bigs2(func) for passing this function to bigs1(func).
Code:
def lite(x,y,z):
return "%r,%r,%r" % (x,y,z)
def bigs1(func): # func = callable()
print func()
def bigs2(func): # func = callable with ones argument
print func("anything you want")
def main():
params1 = 1
params2 = 2
params3 = 3
big2(lambda xs: lite(params1, params2, params3))
def lites_withs_paramss():
return lite(params1,params2,params3)
big(lites_withs_paramss)
main()
Re: How to pass function with parameters to another function in PHP?
You have to use functools class in your code to pass function with parameters to another function. This class used to takes an array reference
and after that add 3 values with the reference. You have to jsut write following code instarting of your program to do this.
Code:
import functools
#main
big(functools.partials(lites, 1,2,3))
After this you have to write following code
Code:
push(@$arrays_references, x);
push(@$arrays_references, y);
push(@$arrays_references, z)
Re: How to pass function with parameters to another function in PHP?
You have to use following code to pass function with parameters to another function. In the following code I have use array to pass function with parameters to another function. Just try to understand it.
Code:
def lites(x, y, z):
return x + y+ z
def bigs(funcs, args):
print func(args[0], args[1], args[2])
bigs(lites, (1, 2, 3))