Re: using return in include files

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

 



On Mon, January 22, 2007 10:11 pm, Aaron Axelsen wrote:
> 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;

The behaviour of return in the main script scope changed from, errr,
PHP 3 to PHP 4, so I'm not sure it's a good idea to rely on it.

> }
> 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?

You *could* do more like this:

if (!defined('TEST_LOADED')){
  define('TEST_LOADED', true);
  function my_func() { return true; }
}

This should work, I think...

You also could consider just using http://php.net/include_once and let
PHP handle this.

Or, you could actually architect your application to not be including
things so willy-nilly that you don't even know what the [bleep] you've
included... :-) :-) :-)

To be blunt, though, code like this usually IS a sign that the
application is a bit dis-organized, pulling in include files with too
little planning.

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

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