Check if a file has been included before? – PHP

  php

Q(Question):

Hello,
I want my scripts to be warning and error free. Is it possible to
check is a file has already been included as part of a "require",
"require_once" or "include" call in the script? I guess I could just
include everything and turn warnings/errors off, but I’d prefer not to
do that. Much thanks –

A(Answer):

if you use requre or include _once() then they should never be included
again.

I do not know if there is a way to check, but years ago, in C i would write
a constant in the include file and check if it was defined in the include if
not, would include itself, then define.


Mike Bradley
http://www.gzentools.com — free online php tools
"D. Alvarado" <la***********@zipmail.com> wrote in message
news:9f**************************@posting.google.c om…

Hello,
I want my scripts to be warning and error free. Is it possible to
check is a file has already been included as part of a "require",
"require_once" or "include" call in the script? I guess I could just
include everything and turn warnings/errors off, but I’d prefer not to
do that. Much thanks –

A(Answer):

if you use requre or include _once() then they should never be included
again.

I do not know if there is a way to check, but years ago, in C i would
write a constant in the include file and check if it was defined in the include
if not, would include itself, then define.

What about

if( !defined(‘INCLUDE_THEFILE’))
{
include( ‘somefile.php’ );
}else{
echo ‘Already included’
}
….
….
// in somefile.php
defined(‘INCLUDE_THEFILE’)

Might not be the best way…i didn’t know that including a file more than
once could be a prob.

Sims

A(Answer):

On 2004-03-07, D. Alvarado wrote:

Hello,
I want my scripts to be warning and error free. Is it possible to
check is a file has already been included as part of a "require",
"require_once" or "include" call in the script? I guess I could just
include everything and turn warnings/errors off, but I’d prefer not to
do that. Much thanks –

How about:
get_included_files () or get_required_files ()

They both return an array containing all included and required files.
They both do the same thing by the way (one is an alias of the other).
See:
http://www.php.net/get_included_files


Mike Peters
mike [-AT-] ice2o [-DOT-] com
http://www.ice2o.com

A(Answer):

"Sims" <si*********@hotmail.com> wrote in message
news:c2*************@ID-162430.news.uni-berlin.de…

if you use requre or include _once() then they should never be included
again.

I do not know if there is a way to check, but years ago, in C i would write

a constant in the include file and check if it was defined in the

include if

not, would include itself, then define.

What about

if( !defined(‘INCLUDE_THEFILE’))
{
include( ‘somefile.php’ );
}else{
echo ‘Already included’
}


// in somefile.php
defined(‘INCLUDE_THEFILE’)

Might not be the best way…i didn’t know that including a file more than
once could be a prob.

Sims

Yea thats it, then I started thinking, they should just use require/include
…._once(); then it hit me, instead of going through all the code, and
writing snippets to see if it was already include, just change all includes
to .._once().

OK, as I write this, I just got an idea, perhaps a small file that shows
something cool is included at several places, and they dont want to over do
it, so they need to check if it is included.

Mike Bradley
http://www.gzentools.com — free online php tools

A(Answer):

On 2004-03-07, CountScubula <me@scantek.hotmail.com> wrote:

if you use requre or include _once() then they should never be included
again.

I do not know if there is a way to check, but years ago, in C i would write
a constant in the include file and check if it was defined in the include if
not, would include itself, then define.

I presume it was like

#ifndef FOO_H
#define FOO_H
….
#endif


http://home.mysth.be/~timvw

A(Answer):

"Mike Peters" <o0****************@THIShotmail.com> wrote in message
news:b9******************************@news.teranew s.com…

On 2004-03-07, D. Alvarado wrote:

Hello,
I want my scripts to be warning and error free. Is it possible to
check is a file has already been included as part of a "require",
"require_once" or "include" call in the script? I guess I could just
include everything and turn warnings/errors off, but I’d prefer not to
do that. Much thanks –

How about:
get_included_files () or get_required_files ()

They both return an array containing all included and required files.
They both do the same thing by the way (one is an alias of the other).
See:
http://www.php.net/get_included_files


Mike Peters
mike [-AT-] ice2o [-DOT-] com
http://www.ice2o.com

Why can’t you just use include_once(…) and require_once(…) to include
all files if you know some will be included multiple times?

-Steve

LEAVE A COMMENT