# HEAD, include(_once), bug in variable declaration

- Thread: https://externals.io/message/7951
- Original archive: https://news-web.php.net/php.internals/7951
- Started by Pierre-Alain Joye on 2004-02-18T14:27:56+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #7951 — Pierre-Alain Joye — 2004-02-18T14:27:56+00:00

URL: https://externals.io/message/7951#7951

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

---

## #7952 — Pierre-Alain Joye — 2004-02-18T14:39:10+00:00

URL: https://externals.io/message/7951#7952 · In reply to: #7951

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

---

## #7954 — Antony Dovgal — 2004-02-18T14:44:08+00:00

URL: https://externals.io/message/7951#7954 · In reply to: #7952

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:
> 
> > […]
> 
> 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

---

## #7953 — Hartmut Holzgraefe — 2004-02-18T14:44:10+00:00

URL: https://externals.io/message/7951#7953 · In reply to: #7951

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

---

## #7955 — Pierre-Alain Joye — 2004-02-18T14:53:36+00:00

URL: https://externals.io/message/7951#7955 · In reply to: #7953

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 call
> 
> remember 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

---

## #7956 — Pierre-Alain Joye — 2004-02-18T15:01:56+00:00

URL: https://externals.io/message/7951#7956 · In reply to: #7955

Forget it. That works the same way in php4.

Sorry for the noise :)

pierre
