Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



How to pass function with parameters to another function in PHP?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 27-02-2010
Member
 
Join Date: Nov 2009
Posts: 57
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.

Reply With Quote
  #2  
Old 27-02-2010
Member
 
Join Date: Apr 2008
Posts: 1,944
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)
Reply With Quote
  #3  
Old 27-02-2010
Member
 
Join Date: May 2008
Posts: 2,001
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.
Reply With Quote
  #4  
Old 27-02-2010
Member
 
Join Date: Apr 2008
Posts: 1,993
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()
Reply With Quote
  #5  
Old 27-02-2010
Member
 
Join Date: May 2008
Posts: 2,283
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)
Reply With Quote
  #6  
Old 27-02-2010
Member
 
Join Date: Nov 2005
Posts: 1,323
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))
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to pass function with parameters to another function in PHP?"
Thread Thread Starter Forum Replies Last Post
c# function equivalent to gettime function in javascript Omaar Software Development 4 10-03-2010 09:44 PM
How to pass PHP variable value to jquery function? Kasper Software Development 5 03-03-2010 05:41 PM
How does abstract function differs from virtual function? Maddox G Software Development 5 29-01-2010 10:32 AM
Pass Array into a function Viensterrr Software Development 3 24-12-2009 11:55 AM
Problem with the parameters of JavaScript function Luis234 Software Development 4 16-03-2009 11:12 AM


All times are GMT +5.5. The time now is 03:16 PM.