Calling Functions from Other Functions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zenthoef
Forum Newbie
Posts: 3
Joined: Tue Mar 16, 2010 9:11 pm

Calling Functions from Other Functions

Post by zenthoef »

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!
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Calling Functions from Other Functions

Post by JakeJ »

You have to do one of two things.

The function your calling has to exist on the code BEFORE the function you're using if the functions are in the same file. OR if it's in another file, the include statement needs to be used.

If you are already doing that, check to make sure you're calling all of the parameters of both functions correctly.

Look at this:

function1($a, $b, $c) {
//do something here
}

function2($x, $y, $z) {
function1(1,2,3);
//do something else
}

echo function2(4,5,6);

If you want to provide the variables for function1() when you call function2() you have to write function2() like this:

function2($a,$b,$c,$x,$y,$z) {
function1($a, $b, $c);
//do something
}

echo function2(1,2,3,4,5,6);
//or
echo function2($a,$b,$c,$x,$y,$z);
zenthoef
Forum Newbie
Posts: 3
Joined: Tue Mar 16, 2010 9:11 pm

Re: Calling Functions from Other Functions

Post by zenthoef »

I didn't know the function ha to exist before the calling function if they are in the same file. I will give that a try! Thanks for your help!
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Calling Functions from Other Functions

Post by JakeJ »

Php (and every other language I can think of) is all processed in line from the top down.

If you end up with some sort of weird configuration where you can't figure out which function to call first, just stick it in another file and use the include statement before you need it. Problem solved.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Calling Functions from Other Functions

Post by flying_circus »

JakeJ wrote:The function your calling has to exist on the code BEFORE the function you're using if the functions are in the same file. OR if it's in another file, the include statement needs to be used.
I've been reading those tutorials you linked me to on w3schools.com and I'm pretty sure that's not the case.
Unless of course, you are defining functions inside of other functions or code blocks. If you are, an include wont save you there. You can reference the PHP manual for further clarification:
http://www.php.net/manual/en/functions.user-defined.php

JakeJ wrote:If you end up with some sort of weird configuration where you can't figure out which function to call first, just stick it in another file and use the include statement before you need it. Problem solved.
This is bad advice. :|

zenthoef wrote: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?
Can you show us the code you are having trouble with? Are the functions static? Functions inside of a class are public by default and they are within each others scope, but are referenced with the "$this->" or "self::" (if static) selectors.

Code: Select all

<?php
  class test
  {
    function method_1()
    {
      return $this->method_2();
    }
    
    function method_2()
    {
      return "Hello World";
    }
  }
  
  $objTest = new test();
  print $objTest->method_1();?>
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Calling Functions from Other Functions

Post by JakeJ »

flying_circus, I think you have fallen off the trapeze one too many times.

From the article you referenced: "When a function is defined in a conditional manner such as the two examples shown. Its definition must be processed prior to being called."

And if you look at the examples below that statement, it's exactly the condition I was describing. If function1() has not yet been processed, function2() will fail.

And why not use an include? It could be appropriate at times.

Bad advice? I don't think so.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Calling Functions from Other Functions

Post by Benjamin »

JakeJ wrote:The function your calling has to exist on the code BEFORE the function you're using if the functions are in the same file. OR if it's in another file, the include statement needs to be used.
This is not correct. PHP files are pre-processed into tokens before they are executed. Any portion of the code has full access to any defined functions, regardless of the order.

Code: Select all

 
<?php
 
function foo_one() {
    foo_two();
}
 
foo_one();
 
function foo_two() {
    echo "foo_two() executed\n<br />";  
}
 
Ouput:

Code: Select all

foo_two() executed
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Calling Functions from Other Functions

Post by flying_circus »

JakeJ wrote:flying_circus, I think you have fallen off the trapeze one too many times.
No argument here.

JakeJ wrote:From the article you referenced: "When a function is defined in a conditional manner such as the two examples shown. Its definition must be processed prior to being called."

And if you look at the examples below that statement, it's exactly the condition I was describing. If function1() has not yet been processed, function2() will fail.
Did you look at the examples?

JakeJ wrote:And why not use an include? It could be appropriate at times.
Not in the manner that you are proposing.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Calling Functions from Other Functions

Post by Benjamin »

JakeJ wrote:When a function is defined in a conditional manner such as the two examples shown. Its definition must be processed prior to being called.
What this is referencing are functions that are only defined based on conditional statements. See example 2 for details.
The PHP Manual wrote: Functions need not be defined before they are referenced, except when a function is conditionally defined as shown in the two examples below.

When a function is defined in a conditional manner such as the two examples shown. Its definition must be processed prior to being called.
Post Reply