php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #17055 Must declare function before referencing when in the scope of an if statement
Submitted: 2002-05-06 19:38 UTC Modified: 2002-05-07 02:52 UTC
Votes:8
Avg. Score:3.0 ± 1.7
Reproduced:4 of 5 (80.0%)
Same Version:2 (50.0%)
Same OS:4 (100.0%)
From: spamsucks86 at comcast dot net Assigned:
Status: Suspended Package: Feature/Change Request
PHP Version: 4.1.2 OS: All
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: spamsucks86 at comcast dot net
New email:
PHP Version: OS:

 

 [2002-05-06 19:38 UTC] spamsucks86 at comcast dot net
if (1==1) {
	if (1==2) {
		echo 'blah';
	}

	else { test('test'); } <--RIGHT HERE

	function test ($blah) {
		echo $blah;
	}
}


This script gives the error: Fatal error: Call to undefined function: test() <line 6>. 

PHP should have recognized that since the first if statement was true, all functions in the scope of that if statement should be declared. If I move the function declaration to before I use it, it works. 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-05-07 02:52 UTC] derick@php.net
This is not really a bug, as PHP only 'remembers' functions as soon as they are seen by the compiler.
Because the compiler does not know if this specific branch will be executed, it doesn't add the function to the function table immediately, but waits until it actually 'hits' the function declaration.
Therefore the function will nto be defined at the locate where you get your error.

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. Then it doesn't matter if you define it before or after the call to the function.
I'm making this report a suspended function/request, as I don't see any fast implementation of this.

Derick
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC