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: 2018-03-29 17:16 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: Scripting Engine problem
PHP Version: 4.1.2 OS: All
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
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
 [2018-03-29 17:16 UTC] cmb@php.net
-Package: Feature/Change Request +Package: Scripting Engine problem
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 11:01:28 2024 UTC