Re: Function Declared in Included File Not Being Found

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux