Edit report at https://bugs.php.net/bug.php?id=63156&edit=1
ID: 63156
User updated by: herb at bobbingwide dot com
Reported by: herb at bobbingwide dot com
Summary: using if ( expression ) requires functions to be
declared before calls
Status: Duplicate
Type: Bug
Package: *Programming Data Structures
Operating System: Windows XP
PHP Version: 5.3.17
Block user comment: N
Private report: N
New Comment:
@nicic thanks for the link to the documentation.
I hadn't realised I could use return... having previously failed to get a
working solution with exit :-)
I'll most likely change my code to something like this.
<?php
if ( defined( 'CONSTANT' )) return;
define( 'CONSTANT', true );
a();
function a() {
echo "a" . PHP_EOL;
}
Then I don't have to worry about the final } at the end of the file
So should #17055 be marked as Closed now, rather than Suspended?
Previous Comments:
------------------------------------------------------------------------
[2012-09-25 14:12:44] [email protected]
@herb: You are using a conditional function definitions, in which case PHP
*can't* know which function will be used, at least not in the general case.
Function hoisting only works with "top-level" function declarations.
This is also documented on http://php.net/manual/en/functions.user-defined.php.
See in particular those two lines:
> 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.
A very simple solution to your particular problem (which at the same time will
yield cleaner code) is to abort early if the constant is defined:
<?php
if (defined('CONSTANT')) {
return;
}
// the rest of the file here
------------------------------------------------------------------------
[2012-09-25 14:11:14] herb at bobbingwide dot com
laruence... agreed. That's how I solved it, as documented in my original report.
I was just commenting on the original response in #17055.
It was the unexpected behaviour that threw me more than anything.
------------------------------------------------------------------------
[2012-09-25 12:22:07] [email protected]
hmm, it's not the only way, since you can get it works well by declare it
before reference to it:
<?php
if ( true ) {
function a() {
echo "a" . PHP_EOL;
}
a();
}
------------------------------------------------------------------------
[2012-09-25 11:33:02] herb at bobbingwide dot com
I accept that this is a duplicate of #17055.
And since derick said, in response to #17055, "I don't see any fast
implementation of this.", 10 years ago (May 2002), I'm inclined to agree that
this will have to be dealt with as a documentation problem.
However, in my particular scenario the original response to the problem... "The
only way to prevent this is to move out the function declaration out of the
branch and in the main scope of the script." is not acceptable since the whole
point of the encapsulating code was to prevent the functions from being
multiply
defined.
So I believe the documentation should provide a clear explanation of the
parsing
and interpretation processes involved.
------------------------------------------------------------------------
[2012-09-25 09:46:26] [email protected]
dup to #17055
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=63156
--
Edit this bug report at https://bugs.php.net/bug.php?id=63156&edit=1