Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Include error? pls help 1

Status
Not open for further replies.

Forri

Programmer
Joined
Oct 29, 2003
Messages
479
Location
MT
Hi

I have a php file which has lots of common functions!

The problem is that this file is included in several other PHP script files. Even tough i use the include_once function i still receive an error stating that i'm redeclaring functions!

How can i solve this?

Thanks
Nick
 
include_once in all the scripts! but i wanted to put it only in one place so i can control better but if i do so the script running will not find any of the functions!


Nick
 
What version of PHP are you running? On my 4.3.6 installation, so long as all scripts and inclusions are using include_once(), I get no errors.

You are likely not using include_once() early enough in your script.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
PHP 4.3.4! should i upgrade?

I'm pretty sure i/m using include_once! i'll give it another check!

Tahnks
ncik
 
In general, you should be keeping up with the upgrades. There are always bugfixes that might affect your installation. But I can't say for a fact that 4.3.6 fixes any issues with inclusion.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
hmmm...then its not my day! thanks still for your great help and interest! if you get an idea pls share!

Tahnks
Nick
 
Check on your use of include() versus include_once().

Here's the micro-app I'm testing with:

test_foo.inc:
Code:
<?php

function foo ()
{
	print "foo";
}
?>

test_include2.php:
Code:
<?php
include_once ('test_include.inc');
?>

test_include1.php:
Code:
<?php
include_once ('test_include.inc');
include_once ('test_include2.php');
?>

"test_include1.php" is the entry point. It includes "test_include.inc" then "test_include2.php". "test_include2.php" then also includes "test_include.inc". So "test_include.inc" gets included twice.

So long as I am using include_once(), I get no errors. I can even use include() first then include_once(). But if include_once() is followed by an include(), I get the error.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Plausable...i'm trying everything i can't figure out what it is! do you think if the function extension file is .php should be .inc?

 
With PHP's filesystem functions (which include include[_once]() and require[_once]()), there is no required naming convention.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
yeah i know but i taught maybe that if the include is a .php it will run it before including it!! so but doesn't make a diference! the thing is that i use the function_exist and still it gives an error!

Tahnks anyways ...i'll try to figure out on my own cheers!

Nick

ps. star for your help!
 
I had a similar problem in the past.
The solution - although a hack - was to set a declared constant which indicates that the file was already loaded. The include statements were then put with an IF which checks for the constant.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top