Hello,
Having the scripts pasted below:
include_once fails to declare the variable 'foo'. Replace include_once
by include and it works. I got the same behavior using require and
require_once.
Am I wrong to see that as a bug?
inc.php
<?php
$foo = "var from include";
?>
testinc.php
<?php
function getFoo()
{
static $calls=0;
include_once 'inc.php';
$calls++;
echo "calls:$calls\n";
return $foo;
}
echo getFoo() . "\n";
echo getFoo() . "\n";
?>
hth
pierre
On Wed, 18 Feb 2004 15:27:56 +0100
paj@pearfr.org (Pierre-Alain Joye) wrote:
Hello,
Having the scripts pasted below:
include_once fails to declare the variable 'foo'. Replace include_once
by include and it works. I got the same behavior using require and
require_once.
To be precised: it fails to declare the variable on the 2nd call. The
1st call works.
pierre
On Wed, 18 Feb 2004 15:39:10 +0100
Pierre-Alain Joye paj@pearfr.org wrote:
On Wed, 18 Feb 2004 15:27:56 +0100
paj@pearfr.org (Pierre-Alain Joye) wrote:Hello,
Having the scripts pasted below:
include_once fails to declare the variable 'foo'. Replace include_once
by include and it works. I got the same behavior using require and
require_once.To be precised: it fails to declare the variable on the 2nd call. The
1st call works.
because it doesn't really include this file on the 2nd call.
[quote]
include_once() should be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, and you want to be sure that it is included exactly once to avoid problems with function redefinitions, variable value reassignments, etc.
[/quote]
thus, this is expected behaviour.
WBR,
Antony Dovgal aka tony2001
tony2001@phpclub.net
Pierre-Alain Joye wrote:
Hello,
Having the scripts pasted below:
include_once fails to declare the variable 'foo'. Replace include_once
by include and it works. I got the same behavior using require and
require_once.Am I wrong to see that as a bug?
inc.php
<?php
$foo = "var from include";
?>testinc.php
<?php
function getFoo()
{
static $calls=0;
include_once 'inc.php';
$calls++;
echo "calls:$calls\n";
return $foo;
}
echo getFoo() . "\n";
echo getFoo() . "\n";
?>
it works for the first call to getFoo(),
on any following call include_once
will ignore "inc.php" as it was already
included before, and as $foo is a local
variable to getFoo() it won't exist in
any but the first call
remember that in PHP include is evaluated
at runtime, not at compile time ...
--
Hartmut Holzgraefe <hartmut@php.net
On Wed, 18 Feb 2004 15:44:10 +0100
hartmut@php.net (Hartmut Holzgraefe) wrote:
it works for the first call to getFoo(),
on any following call include_once
will ignore "inc.php" as it was already
included before, and as $foo is a local
variable to getFoo() it won't exist in
any but the first callremember that in PHP include is evaluated
at runtime, not at compile time ...
Thanks for the reminder :) I thought about that at the first source of
the problem, but:
It works with php4. For instance this is widely used to include a given
file on demand, and only on demand.
I thought that in the context of getFoo (lost between 2 calls), it
should include it again.
In this simple case, it is not a problem, there is no global
"pollution". But in the case the included files contains a
class/function definition, you run in troubles. That's why I think the
behavioir should remain the same as with php4.
An alternative is to split all possible runtime datas to a different
file and not in the same file as the class/function declaration. But in
both cases that will break a lot of scripts around.
hth
pierre
Forget it. That works the same way in php4.
Sorry for the noise :)
pierre