Results 1 to 6 of 6

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

  1. #1
    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.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,949

    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)

  3. #3
    Join Date
    May 2008
    Posts
    2,015

    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.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,010

    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()

  5. #5
    Join Date
    May 2008
    Posts
    2,302

    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)

  6. #6
    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))

Similar Threads

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •