Re: function

From: Date: Sat, 05 May 2012 01:38:36 +0000
Subject: Re: function
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-317794@lists.php.net to get a copy of this message
On Thu, May 3, 2012 at 9:12 PM, Ron Piggott <ron.piggott@actsministries.org> wrote: > I need to access a FUNCTION I programmed within a different FUNCTION.  Are these able to be > passed like a variable?  Or are they able to become like a $_SESSION variable in nature?  How am I > able to do this? > > I am essentially programming: > > === > function name( $flag1, $flag2 ) { > > # some PHP > > echo name_of_a_different_function( $flag1 , $flag2 ); > > } > === > > The error I am receiving is “Call to undefined function name_of_a_different_function” Where is name_of_a_different_function defined? If it is somewhere in the same file as name, that shouldn't be a problem, provided it is defined in the same namespace/scope as name. If it is defined in a different file, you need to include that file before you make the echo statement. For example: function func1 ($flag1, $flag2) { # blah blah echo func2($flag1, $flag2); } function func2 ($flag1, $flag2) { #blah blah return "some string value"; } in the same file should be just fine. It doesn't really matter what order func1 and func2 are declared in. However, if func2 is defined in some_other_file.php, you need to include it in this_file.php (where func1 is defined) first: this_file.php: include('some_other_file.php'); function func1 ($flag1, $flag2) { #blah blah echo func2 ($flag1, $flag2); } some_other_file.php: function func2 ($flag1, $flag2) { #blah blah return "some string value"; } If func2 is a method for an object/class, you'll have to access it that way in func1: this_file.php: include('MyClass.php'); function func1 ($flag1, $flag2) { # blah blah, instantiate object? $myobj = new MyClass(); echo $myobj->func2 ($flag1, $flag2); } MyClass.php: class MyClass { function func2 ($flag1, $flag2) { #blah blah return "some string value"; } }

« previous php.general (#317794) next »