RE: using return in include files

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



-----Original Message-----
From: Aaron Axelsen [mailto:lists@xxxxxxxxxxxx]
Sent: 23 January 2007 06:12
To: php-general@xxxxxxxxxxxxx
Subject:  using return in include files


I'm trying to figure out what the desired behavior is of using the
return function to bail out of an include page.

I did some testing, and this is what I concluded.

First, I created the following file:

<?php
if (defined('TEST_LOADED')) {
        return;
}
define('TEST_LOADED',true);
echo "blah blah blah blah<br/>";
?>

I then called it as follows:
include('test.php');
include('test.php');
include('test.php');

The output is:
blah blah blah blah

Second, I changed the test.php file to be the following:

<?php
if (defined('TEST_LOADED')) {
        return;
}
define('TEST_LOADED',true);
echo "blah blah blah blah<br/>";

function myFunc($test) {

}
?>

When I load the page now, it throws the following error: PHP Fatal
error: Cannot redeclare myfunc()

It appears that if there are functions in the include page that you
can't use return to bail out.  What is the desired functionality in this
case?  Is this a bug in how php handles it? or was return never designed
to be used this way?

Any thoughts are appreciated.

--
Aaron Axelsen
lists@xxxxxxxxxxxx

------------------------------------

Include works as though the "include" statement line is replaced by the code
that is being included. So try the following exercise and it should become
obvious why you're getting that error: copy-and-paste the code from test.php
into the code that "includes" test.php for each line that includes test.php,
and then delete each of those include lines (I'll comment them out). You end
up with:

<?php
// include('test.php');
if (defined('TEST_LOADED')) {
        return;
}
define('TEST_LOADED',true);
echo "blah blah blah blah<br/>";

function myFunc($test) {

}

// include('test.php');
if (defined('TEST_LOADED')) {
        return;
}
define('TEST_LOADED',true);
echo "blah blah blah blah<br/>";

function myFunc($test) {

}

// include('test.php');
if (defined('TEST_LOADED')) {
        return;
}
define('TEST_LOADED',true);
echo "blah blah blah blah<br/>";

function myFunc($test) {

}
?>

Run that and you should get exactly the same error. Interestingly under php4
neither your code nor my expanded version of your code throws an error, so I
assume you're using php5?

If you're trying to avoid including the file more than once you could use
"include_once", but I've had problems with that - the file was previously
included but not in the same scope, so it wasn't included second time it was
called and yet none of the included file's contents existed. The way I got
round it was by assigning a value in the include file, and then testing for
that value before including the file.

For example:

test.php
<?php
$testphp_loaded = TRUE; // <- must be outside any function or braces
.
.
.
?>

main.php
<?php
if (!$testphp_loaded) {
    require('test.php');
}
?>

You can't use a define for this because it has a much wider scope and you
end up with the same problem as using "include_once".

Arno

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux