www.webdeveloper.com
Results 1 to 8 of 8

Thread: Calling Functions from Other Functions

  1. #1
    Join Date
    Dec 2008
    Posts
    51

    Calling Functions from Other Functions

    I'm sure its possible to call functions from other functions, but it is not working for me. Is there some syntax someone can provide to me as an example of calling a function within another function?

    Also, both of these functions are in the same class. I have not declared any functions in this class as public or private. Do functions in a class default to private? Or do functions default to public? If they are public by default, are functions in the same class in each other's scope?

    Thanks!

  2. #2
    Join Date
    May 2003
    Posts
    599
    Here is a prior discussion with a little relevance:
    http://www.webdeveloper.com/forum/sh...lling+function

    I think public is default..., but i also think that public and private visibility declaration on functions is only available as of php 5, and a lot of people still use php 4...so you may not need to declare depending on your target and enough planning.

    There is a good resource on visibility here:
    http://www.webdeveloper.com/forum/sh...lling+function
    Last edited by hastx; 03-16-2010 at 10:39 PM.

  3. #3
    Join Date
    Mar 2010
    Posts
    2,803
    yes, private and public delarations of class variables and functions is available in php5

    Imo it's good practice to declare a variable or function private/public regardless of the default....but that's just me

    To call a function with a class file in php5 use:

    Code:
    $this->functionName(arg1,arg2,argN);
    Last edited by tirna; 03-16-2010 at 11:24 PM.

  4. #4
    Join Date
    Dec 2008
    Posts
    51
    Thanks for the replies. I don't think I was clear in what I was asking. Basically, is this possible:

    Code:
    class someClass{
    
    function_one($arg1)
    {
      $output_one = $arg1+15;
      return $output_one;
    }
    
    function_two()
    {
      $send = 3;
      $output_two = function_one($send);
      return $output_two;
    }
    }
    My question is if i call funtion_two will it return a value of 18? If this is possible is my syntax right? I'm basically doing this, but more complicated and its not working.

    Thanks.

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,528
    A function defined in a class does not "exist" only by its function name, you must reference it via the "->" operator as a member of an object, or statically via the "::" operator as a class member. Therefore in your example you would want to use the special "$this" variable to indicate that the function is a method of the current object:
    PHP Code:
    $output_two $this->function_one($send); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Mar 2010
    Posts
    2,803
    Quote Originally Posted by NogDog View Post
    A function defined in a class does not "exist" only by its function name, you must reference it via the "->" operator as a member of an object, or statically via the "::" operator as a class member. Therefore in your example you would want to use the special "$this" variable to indicate that the function is a method of the current object:
    PHP Code:
    $output_two $this->function_one($send); 
    Correct me if I'm wrong, but isn't the above what was said earlier

    Code:
     
     
    yes, private and public delarations of class variables and functions is available in php5
    
    Imo it's good practice to declare a variable or function private/public regardless of the default....but that's just me :)
    
    To call a function with a class file in php5 use:
    
    
    Code:
    $this->functionName(arg1,arg2,argN);

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,528
    Quote Originally Posted by tirna View Post
    Correct me if I'm wrong, but isn't the above what was said earlier
    Yes, it was said, but apparently not understood, so I tried again in the hopes that this time it might be.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  8. #8
    Join Date
    Dec 2008
    Posts
    51
    I did misunderstand that post! However, the "$this->" solved my problems. Thanks so much for all who answered!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles