require_once() question for PHP programmers with C or C++ background

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I am trying to understand how require_once (generally including files in
PHP) works. I understand the general concept - a bit similar to header
files in C/C++, but I am a bit baffled by PHPs behaviour. I have two
files (FILE 1 and FILE2):

This file requires some constants defined in constants.php, as well as a
classes defined in another file. file1 includes constants.php (via
require_once()). This file then defines a class CLASS1.

This second file needs a CLASS1 defined in FILE1, so I include FILE2 in
FILE1 (again, via a require_once()).

However, when I run the second script (FILE2), I have several warnings like:

Notice: Use of undefined constant COOKIE_NAME - assumed 'COOKIE_NAME' in

The only way to get rid of the warnings is to include the same
constant.php file again (via a require_once()) in FILE2. This seems
unnecessary, if FILE2 "pulls in" FILE1 which then "pulls in"
constants.php - why are the constants not defined?. The only possible
explanation could be something to do with the order in which the files
are included - it would seem that the parent file is very loaded and
parsed before any includes are "included" - but that seems to defeat the
whole purpose of includes  - since the data imported would not be
available in the "first parse".

Unless ofcourse, the warnings are meant to be ignored (Please somebody
say it isn't so - otherwise this would be far too ridiculous).

I use C/C++ so demonstrate my self better with fellow coders who may
have a similar background.

I can have the following class declarations
Lets assume (for the sake of brevity) that Header_Start(X) and
Header_End(X) are header include guard macros.

void foo();
std::string  foobar(const std::string&);

#include "header1.h"
int dosomething();
void* getrawpointer();

//Note: I only need to #include header2.h, since it "pulls in" header1.h
#include "Header2.h"
// .. impl here ..

I was under the impression that this how PHP includes (or more
specifically require_once()) works?

If this is not the case - then does that mean that for a given file, I
will have to include all files that are nested includes in the file?

Re: require_once() question for PHP programmers with C or C++ background

Ronald Raygun wrote:
Quoted text here. Click to load it

Since you are a C programmer, think of the #ifndef statement.  require
(or include) does an IN-LINE INCLUDE of that named file.  Using
require_once (or include_once) is a [very] little like surrounding that
statement with a  preprocessor set, #ifndef #endif.  In other words, if
it has already been included, it is not included again but if it hasn't
then it is included.  So, you should generally use the "_once" forms
except where you actually want it in more than one place.  For example,
suppose you have the months of the year bracketed by <option></option>
tags for a dropdown list and have that in an include file.  Then suppose
you need that dropdown list in more than one place on a given page.  You
would then not use the "_once" form.

In general think of the require (or include) or their "_once" forms as
sort of a kind of pseudocode.  It keeps the file a manageable length and
allows you to put blocks of code into separate files.  In the end it
makes no difference as they are all put together as one long sequence at
interpreter (run) time.

Does that help you any?

Re: require_once() question for PHP programmers with C or C++ background

sheldonlg wrote:

Quoted text here. Click to load it

Hi sheldon, Yes, what you describe (i.e. aka "include guards") is what
my understanding on require_once is. However, the behaviour I observed
(described above) contradicts my understanding.

BTW, I am running PHP 5.2.5 on W2k.  I did a google search on nested
includes and it appears there is a PHP bug for PHP 5.2.4 for nested
includes. I am not sure if this issue was resolved for v5.2.5.

For now, I will simply have to put up with including files again (if
even they are included via a nested include. I have learnt to be have a
more "carefree" attitude about things when using PHP - its very much the
philosophy of PHP I feel - reminds me of Perl's TMTOWTDI (which BTW I
hate a pathological hatred of) ... but thats another story ...

For now, I'll just "double include", so I can get this damn script
working. Its simply not worth spending a whole day (which I have),
trying to figure it out.

Re: require_once() question for PHP programmers with C or C++ background

Ronald Raygun wrote:

Quoted text here. Click to load it

Sounds to me that it's a PHP newbie error, not a include() issue.

How are you referring to array elements? Are you enclosing the array key
between quotes?

e.g., instead of:


Could you please check for that (deprecated) syntax in your code, please?

Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Gemelo intenta suicidarse y mata a su hermano por error.

Site Timeline