Check if a file has been included before?

This is a discussion on Check if a file has been included before? within the PHP Language forums, part of the PHP Programming Forums category; Hello, I want my scripts to be warning and error free. Is it possible to check is a file has ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-07-2004
D. Alvarado
 
Posts: n/a
Default Check if a file has been included before?

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 -
Reply With Quote
  #2 (permalink)  
Old 03-07-2004
CountScubula
 
Posts: n/a
Default Re: Check if a file has been included before?

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" <laredotornado@zipmail.com> wrote in message
news:9fe1f2ad.0403061949.1d74ebb6@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 -
>



Reply With Quote
  #3 (permalink)  
Old 03-07-2004
Sims
 
Posts: n/a
Default Re: Check if a file has been included before?


> 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


Reply With Quote
  #4 (permalink)  
Old 03-07-2004
Mike Peters
 
Posts: n/a
Default Re: Check if a file has been included before?

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
Reply With Quote
  #5 (permalink)  
Old 03-07-2004
CountScubula
 
Posts: n/a
Default Re: Check if a file has been included before?

"Sims" <siminfrance@hotmail.com> wrote in message
news:c2f05b$1rr9hh$1@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


Reply With Quote
  #6 (permalink)  
Old 03-08-2004
Tim Van Wassenhove
 
Posts: n/a
Default Re: Check if a file has been included before?

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
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 07:27 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0