|
|
 |
 |
 |
 |
Function Declared in Included File Not Being Found
Hello, According to the PHP manual on functions (http://www.php.net/manual/ en/language.functions.php): "In PHP 3, functions must be defined before they are referenced. No such requirement exists since PHP 4. Except when a function is conditionally defined..." If that is true then why does the following not work as I expect? I expect the result to be "Function was called!" but it actually is "Function test() does not exist!". File: a.php <?php if (function_exists('test')) { echo test(); }
else { echo "Function test() does not exist!"; }
require 'b.php'; ?> ------------------------------- File: b.php <?php require 'c.php'; ?> ------------------------------- File: c.php <?php function test() { return "Function was called!"; }
?> Chris
<?php require 'b.php'; if (function_exists('test')) { echo test(); } else { echo "Function test() does not exist!"; } ?> You cant drive till after you buy the car -- If at first you dont succeed try try try again If at first you do succeed try not to look surprised _ "Chris" <c @dented-planet.net> wrote in message news:527FCBE4-236F-48F0-AA36-2A5575FFCA48@dented-planet.net...
> Hello, > According to the PHP manual on functions (http://www.php.net/manual/ > en/language.functions.php): > "In PHP 3, functions must be defined before they are referenced. No > such requirement exists since PHP 4. Except when a function is > conditionally defined..." > If that is true then why does the following not work as I expect? > I expect the result to be "Function was called!" but it actually is > "Function test() does not exist!". > File: a.php > <?php > if (function_exists('test')) { > echo test(); > } > else { > echo "Function test() does not exist!"; > } > require 'b.php'; > ?> > ------------------------------- > File: b.php > <?php > require 'c.php'; > ?> > ------------------------------- > File: c.php > <?php > function test() { > return "Function was called!"; > } > ?> > Chris
On May 11, 2007, at 1:43 PM, Chris wrote:
> Hello, > According to the PHP manual on functions > (http://www.php.net/manual/en/language.functions.php): > "In PHP 3, functions must be defined before they are referenced. No > such requirement exists since PHP 4. Except when a function is > conditionally defined..." > If that is true then why does the following not work as I expect? > I expect the result to be "Function was called!" but it actually is > "Function test() does not exist!". > File: a.php > <?php > if (function_exists('test')) { > echo test(); > } > else { > echo "Function test() does not exist!"; > } > require 'b.php'; > ?> > ------------------------------- > File: b.php > <?php > require 'c.php'; > ?> > ------------------------------- > File: c.php > <?php > function test() { > return "Function was called!"; > } > ?> > Chris
One reason I believe is that including a file in an included file will not work. file a includes file b wnich includes file c, code in file c will not register. (if I remember the manual correctly) Jeff K -----------------------------------------------Reply-----------------------------------------------
Files are included/required at run-time. As such, the function has not been declared when you make reference to it since the require occurs later. Move the require to the top. As a test, feel free to explicitly define the function at the bottom without using a require. Cheers, Rob.
On Fri, 2007-05-11 at 16:43 -0400, Chris wrote: > Hello, > According to the PHP manual on functions (http://www.php.net/manual/ > en/language.functions.php): > "In PHP 3, functions must be defined before they are referenced. No > such requirement exists since PHP 4. Except when a function is > conditionally defined..." > If that is true then why does the following not work as I expect? > I expect the result to be "Function was called!" but it actually is > "Function test() does not exist!". > File: a.php > <?php > if (function_exists('test')) { > echo test(); > } > else { > echo "Function test() does not exist!"; > } > require 'b.php'; > ?> > ------------------------------- > File: b.php > <?php > require 'c.php'; > ?> > ------------------------------- > File: c.php > <?php > function test() { > return "Function was called!"; > } > ?> > Chris
-- .------------------------------------------------------------. | InterJinn Application Framework - http://www.interjinn.com | :------------------------------------------------------------: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `------------------------------------------------------------' -----------------------------------------------Reply-----------------------------------------------
There was a specific reason that the require to the file defining the function came after the call to the function. But alas, it doesn't matter what the reason is now. Thanks for the explanation. Chris On May 11, 2007, at 7:53 PM, Robert Cummings wrote:
> Files are included/required at run-time. As such, the function has not > been declared when you make reference to it since the require occurs > later. Move the require to the top. As a test, feel free to explicitly > define the function at the bottom without using a require. > Cheers, > Rob. > On Fri, 2007-05-11 at 16:43 -0400, Chris wrote: >> Hello, >> According to the PHP manual on functions (http://www.php.net/manual/ >> en/language.functions.php): >> "In PHP 3, functions must be defined before they are referenced. No >> such requirement exists since PHP 4. Except when a function is >> conditionally defined..." >> If that is true then why does the following not work as I expect? >> I expect the result to be "Function was called!" but it actually is >> "Function test() does not exist!". >> File: a.php >> <?php >> if (function_exists('test')) { >> echo test(); >> } >> else { >> echo "Function test() does not exist!"; >> } >> require 'b.php'; >> ?> >> ------------------------------- >> File: b.php >> <?php >> require 'c.php'; >> ?> >> ------------------------------- >> File: c.php >> <?php >> function test() { >> return "Function was called!"; >> } >> ?> >> Chris > -- > .------------------------------------------------------------. > | InterJinn Application Framework - http://www.interjinn.com | > :------------------------------------------------------------: > | An application and templating framework for PHP. Boasting | > | a powerful, scalable system for accessing system services | > | such as forms, properties, sessions, and caches. InterJinn | > | also provides an extremely flexible architecture for | > | creating re-usable components quickly and easily. | > `------------------------------------------------------------'
On Fri, May 11, 2007 3:43 pm, Chris wrote: > Hello, > According to the PHP manual on functions (http://www.php.net/manual/ > en/language.functions.php): > "In PHP 3, functions must be defined before they are referenced. No > such requirement exists since PHP 4. Except when a function is > conditionally defined..."
Does moving the require BEFORE the if/else change anything? > If that is true then why does the following not work as I expect?
Not sure, really... Seems like you're right, but maybe I'm also missing something. If you take out the 'require' statements, and just put c inside a, does it work? > I expect the result to be "Function was called!" but it actually is > "Function test() does not exist!".
Perhaps the if (function_exists()) business is trying to be "smart" and is run because at the time it was compiled, the function didn't exist, so you could define the function there -- which is a much more common idiom than what you are doing, truth to tell... -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So?
-----------------------------------------------Reply-----------------------------------------------
On Fri, May 11, 2007 6:15 pm, jekillen wrote: > One reason I believe is that including a file in an included file will > not work. > file a includes file b wnich includes file c, code in file c will not > register. (if > I remember the manual correctly)
I dunno what you were reading, but almost for sure, you've interpreted it incorrectly... :-) -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So?
-----------------------------------------------Reply-----------------------------------------------
Richard, Neither removing the if/else nor the function_exists() not call worked. If I declared the function in the same file I called it works even when defining it after the call. If the function is declaring in a different file than the one I call it in and I include the files after the call, it simply doesn't work. Unless I did something subtly wrong or am back on the drugs. This SEEMS contrary to the documentation. The "issue" occurs in PHP-CLI 4.4.4, PHP 5.2.1, and PHP 5.2.2. I'll files a bug report in the hope of an explanation but I'm pretty sure that it isn't considered a bug. Chris On May 13, 2007, at 1:38 AM, Richard Lynch wrote:
> On Fri, May 11, 2007 3:43 pm, Chris wrote: >> Hello, >> According to the PHP manual on functions (http://www.php.net/manual/ >> en/language.functions.php): >> "In PHP 3, functions must be defined before they are referenced. No >> such requirement exists since PHP 4. Except when a function is >> conditionally defined..." > Does moving the require BEFORE the if/else change anything? >> If that is true then why does the following not work as I expect? > Not sure, really... > Seems like you're right, but maybe I'm also missing something. > If you take out the 'require' statements, and just put c inside a, > does it work? >> I expect the result to be "Function was called!" but it actually is >> "Function test() does not exist!". > Perhaps the if (function_exists()) business is trying to be "smart" > and is run because at the time it was compiled, the function didn't > exist, so you could define the function there -- which is a much more > common idiom than what you are doing, truth to tell... > -- > Some people have a "gift" link here. > Know what I want? > I want you to buy a CD from some indie artist. > http://cdbaby.com/browse/from/lynch > Yeah, I get a buck. So?
Chris wrote: > Richard, > Neither removing the if/else nor the function_exists() not call worked. > If I declared the function in the same file I called it works even when > defining it after the call. > If the function is declaring in a different file than the one I call it > in and I include the files after the call, it simply doesn't work. > Unless I did something subtly wrong or am back on the drugs. > This SEEMS contrary to the documentation. > The "issue" occurs in PHP-CLI 4.4.4, PHP 5.2.1, and PHP 5.2.2. > I'll files a bug report in the hope of an explanation but I'm pretty > sure that it isn't considered a bug.
It's not contrary to the documentation. The docs say that a function does not need to be declared before use - this is true, so long as it's declared in the same file. When PHP parses a file it first looks through it looking for declarations and adds them to the global scope. It then goes through it again executing the code. Require/include and their _once versions don't get executed until the second pass, and they get done at the point where they're called. So, if you're going to use functions in an included file, you need to include that file before trying to use them. If you're using functions in the same file, the order of things in that file does not matter. This is *not* a bug!! Hope that clears it up. -Stut
> On May 13, 2007, at 1:38 AM, Richard Lynch wrote: >> On Fri, May 11, 2007 3:43 pm, Chris wrote: >>> Hello, >>> According to the PHP manual on functions (http://www.php.net/manual/ >>> en/language.functions.php): >>> "In PHP 3, functions must be defined before they are referenced. No >>> such requirement exists since PHP 4. Except when a function is >>> conditionally defined..." >> Does moving the require BEFORE the if/else change anything? >>> If that is true then why does the following not work as I expect? >> Not sure, really... >> Seems like you're right, but maybe I'm also missing something. >> If you take out the 'require' statements, and just put c inside a, >> does it work? >>> I expect the result to be "Function was called!" but it actually is >>> "Function test() does not exist!". >> Perhaps the if (function_exists()) business is trying to be "smart" >> and is run because at the time it was compiled, the function didn't >> exist, so you could define the function there -- which is a much more >> common idiom than what you are doing, truth to tell... >> --Some people have a "gift" link here. >> Know what I want? >> I want you to buy a CD from some indie artist. >> http://cdbaby.com/browse/from/lynch >> Yeah, I get a buck. So? > --PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php
|
 |
 |
 |
 |
|