By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,402 Members | 1,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,402 IT Pros & Developers. It's quick & easy.

require_once doesn't work, "cannot redeclare class..."

P: n/a
I'm flipping my wig here, people. I'm using classes and making each class a
file. when I'm including dependet classess, I use require_once to avoid
multiple declarations - yet they happen. I put debug_print_backtrace in the
file to see how it is included, and here's the output:
#0 require_once() called at [\eKirje.textGrid.class.php:4]
#1 require_once(\eKirje.textGrid.class.php) called at
[\lasku.eKirjeLasku.class.php:3]#0 require_once() called at
[\eKirje.kanava.class.php:3]
#1 require_once(\eKirje.kanava.class.php) called at
[\eKirje.EPL8.class.php:3]
#2 require_once(\eKirje.EPL8.class.php) called at
[\eKirje.kirje.class.php:3]
#3 require_once(\eKirje.kirje.class.php) called at
[\lasku.eKirjeLasku.class.php:5]
<br />
<b>Fatal error</b>: Cannot redeclare class boxcontainer in
<b>\eKirje.boxcontainer.class.php</b> on line <b>5</b><br />As you see, it
does get required twice regardless of the use of require_once in each call.
And eventually the class gets declared again. My fix for the problem was to
use

if( !in_array('boxcontainer', get_declared_classes()) ) {
require_once('eKirje.boxContainer.class.php');
}

in the files and now it works, but I'm just totally baffeld of why this is
happening? How come the require_once fails to function? Am I missing
something here?

I made the simplest test case where I had four files where in the first of
them I declare a class, then require_once it to two other files and then
finally require_once the two files to a fourth file. In this case I did not
get redeclaration errors, for some reason it worked okay then, the class was
declared only one and it worked okay.

--
"En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)
Feb 24 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Kimmo Laine wrote:
I'm flipping my wig here, people. I'm using classes and making each class
a file. when I'm including dependet classess, I use require_once to avoid
multiple declarations - yet they happen. I put debug_print_backtrace in
the file to see how it is included, and here's the output:
#0 require_once() called at [\eKirje.textGrid.class.php:4]
#1 require_once(\eKirje.textGrid.class.php) called at
[\lasku.eKirjeLasku.class.php:3]#0 require_once() called at
[\eKirje.kanava.class.php:3]
#1 require_once(\eKirje.kanava.class.php) called at
[\eKirje.EPL8.class.php:3]
#2 require_once(\eKirje.EPL8.class.php) called at
[\eKirje.kirje.class.php:3]
#3 require_once(\eKirje.kirje.class.php) called at
[\lasku.eKirjeLasku.class.php:5]
<br />
<b>Fatal error</b>: Cannot redeclare class boxcontainer in
<b>\eKirje.boxcontainer.class.php</b> on line <b>5</b><br />As you see, it
does get required twice regardless of the use of require_once in each
call. And eventually the class gets declared again. My fix for the problem
was to use

if( !in_array('boxcontainer', get_declared_classes()) ) {
require_once('eKirje.boxContainer.class.php');
}

in the files and now it works, but I'm just totally baffeld of why this is
happening? How come the require_once fails to function? Am I missing
something here?

I made the simplest test case where I had four files where in the first of
them I declare a class, then require_once it to two other files and then
finally require_once the two files to a fourth file. In this case I did
not get redeclaration errors, for some reason it worked okay then, the
class was declared only one and it worked okay.

Hi,

require and require_once act on FILES, not on their content.
If the file contains an objectdefinition is of no concern.

so suppose you have:
file1.php containing object X
file2.php ALSO containing object X

Then:
include_once('file1.php);
include_once('file2.php);

will result in a double objectX declaration.
Could your problem be caused by something like this?
Do you maybe have the same class in different files?

Regards,
Erwin Moller
Feb 24 '06 #2

P: n/a
"Erwin Moller"
<si******************************************@spam yourself.com> wrote in
message news:43***********************@news.xs4all.nl...
Kimmo Laine wrote:
I'm flipping my wig here, people. I'm using classes and making each class
a file. when I'm including dependet classess, I use require_once to avoid
multiple declarations - yet they happen. I put debug_print_backtrace in
the file to see how it is included, and here's the output:
#0 require_once() called at [\eKirje.textGrid.class.php:4]
#1 require_once(\eKirje.textGrid.class.php) called at
[\lasku.eKirjeLasku.class.php:3]#0 require_once() called at
[\eKirje.kanava.class.php:3]
#1 require_once(\eKirje.kanava.class.php) called at
[\eKirje.EPL8.class.php:3]
#2 require_once(\eKirje.EPL8.class.php) called at
[\eKirje.kirje.class.php:3]
#3 require_once(\eKirje.kirje.class.php) called at
[\lasku.eKirjeLasku.class.php:5]
<br />
<b>Fatal error</b>: Cannot redeclare class boxcontainer in
<b>\eKirje.boxcontainer.class.php</b> on line <b>5</b><br />As you see,
it
does get required twice regardless of the use of require_once in each
call. And eventually the class gets declared again. My fix for the
problem
was to use

if( !in_array('boxcontainer', get_declared_classes()) ) {
require_once('eKirje.boxContainer.class.php');
}

in the files and now it works, but I'm just totally baffeld of why this
is
happening? How come the require_once fails to function? Am I missing
something here?

I made the simplest test case where I had four files where in the first
of
them I declare a class, then require_once it to two other files and then
finally require_once the two files to a fourth file. In this case I did
not get redeclaration errors, for some reason it worked okay then, the
class was declared only one and it worked okay.
Hi,

require and require_once act on FILES, not on their content.
If the file contains an objectdefinition is of no concern.


Yes, this is exactly how I've understood it, there's no problem here.
so suppose you have:
file1.php containing object X
file2.php ALSO containing object X

Then:
include_once('file1.php);
include_once('file2.php);
No, see below.
will result in a double objectX declaration.
Could your problem be caused by something like this?
Do you maybe have the same class in different files?

No, absolutely not. I've got a file A.php containing class A and files F.php
and G.php which both have require_once("A.php"); Then I have a page D.php
which has require_once("G.php"); and require_once("F.php"); resulting the
multiple declaration, since they both eventually require A.php, which they
shouldn't since I've used require_once...

--
"En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)
Feb 24 '06 #3

P: n/a
Kimmo Laine wrote:

<snip>

No, absolutely not. I've got a file A.php containing class A and files
F.php and G.php which both have require_once("A.php"); Then I have a page
D.php which has require_once("G.php"); and require_once("F.php");
resulting the multiple declaration, since they both eventually require
A.php, which they shouldn't since I've used require_once...


aha. That is the problem then, but it is subtile.

This what PHP.net says about require_once():
The require_once() statement includes and evaluates the specified file
during the execution of the script. This is a behavior similar to the
require() statement, with the only difference being that if the code from a
file has already been included, it will not be included again. See the
documentation for require() for more information on how this statement
works.
So you might expect that PHP is counting the number os time a certain file
is required, but that is NOT what it says: It only look for THE SAME FILE.

The difference might not seem big, but this is excactly what causes your
problem: Your include is used in different files, and thus included every
time, hence the multiple declaration.

Regards,
Erwin Moller
Feb 24 '06 #4

P: n/a
Kimmo Laine wrote:
I'm flipping my wig here, people. I'm using classes and making each class a
file. when I'm including dependet classess, I use require_once to avoid
multiple declarations - yet they happen. I put debug_print_backtrace in the
file to see how it is included, and here's the output:
#0 require_once() called at [\eKirje.textGrid.class.php:4]
#1 require_once(\eKirje.textGrid.class.php) called at
[\lasku.eKirjeLasku.class.php:3]#0 require_once() called at
[\eKirje.kanava.class.php:3]
#1 require_once(\eKirje.kanava.class.php) called at
[\eKirje.EPL8.class.php:3]
#2 require_once(\eKirje.EPL8.class.php) called at
[\eKirje.kirje.class.php:3]
#3 require_once(\eKirje.kirje.class.php) called at
[\lasku.eKirjeLasku.class.php:5]
<br />
<b>Fatal error</b>: Cannot redeclare class boxcontainer in
<b>\eKirje.boxcontainer.class.php</b> on line <b>5</b><br />As you see, it
does get required twice regardless of the use of require_once in each call.
And eventually the class gets declared again. My fix for the problem was to
use

if( !in_array('boxcontainer', get_declared_classes()) ) {
require_once('eKirje.boxContainer.class.php');
}

in the files and now it works, but I'm just totally baffeld of why this is
happening? How come the require_once fails to function? Am I missing
something here?

I made the simplest test case where I had four files where in the first of
them I declare a class, then require_once it to two other files and then
finally require_once the two files to a fourth file. In this case I did not
get redeclaration errors, for some reason it worked okay then, the class was
declared only one and it worked okay.


Kimmo,

That should work just fine.

First of all, please check the case on each of your require_once
statements. Are they the same? If the case is different, PHP will
think it's two different files. Also if the path has changed PHP may
consider them to be different files.

Perhaps if you copy/paste the actual require_once statements so we can
see exactly what you're doing.

Also, what version of PHP are you using?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 24 '06 #5

P: n/a
Erwin Moller wrote:

So you might expect that PHP is counting the number os time a certain file
is required, but that is NOT what it says: It only look for THE SAME FILE.

The difference might not seem big, but this is excactly what causes your
problem: Your include is used in different files, and thus included every
time, hence the multiple declaration.

No, PHP has more intelligence when it comes to require_once than that, and
will recognise that they are both the same file, even though they are being
included via 2 different files.
This even holds true if you chdir('somedir') and use a different path to
include the same file.

--
Tommy
http://design.twobarks.com/
Feb 24 '06 #6

P: n/a
Kimmo Laine wrote:
I'm flipping my wig here, people. I'm using classes and making each class
a file. when I'm including dependet classess, I use require_once to avoid
multiple declarations - yet they happen. I put debug_print_backtrace in
the file to see how it is included, and here's the output:
#0 require_once() called at [\eKirje.textGrid.class.php:4]
#1 require_once(\eKirje.textGrid.class.php) called at
[\lasku.eKirjeLasku.class.php:3]#0 require_once() called at
[\eKirje.kanava.class.php:3]
#1 require_once(\eKirje.kanava.class.php) called at
[\eKirje.EPL8.class.php:3]
#2 require_once(\eKirje.EPL8.class.php) called at
[\eKirje.kirje.class.php:3]
#3 require_once(\eKirje.kirje.class.php) called at
[\lasku.eKirjeLasku.class.php:5]
<br />
<b>Fatal error</b>: Cannot redeclare class boxcontainer in
<b>\eKirje.boxcontainer.class.php</b> on line <b>5</b><br />As you see, it
does get required twice regardless of the use of require_once in each
call. And eventually the class gets declared again. My fix for the problem
was to use

if( !in_array('boxcontainer', get_declared_classes()) ) {
require_once('eKirje.boxContainer.class.php');
}

Trying to decipher that backtrace, it looks to me like you are including
several different class files. Does any of those depend on the
Kirje.boxcontainer.class.php, and include it on it's own? Could it be that
you at some other place are including the file using plain include or
require?
--
Tommy
http://design.twobarks.com/
Feb 24 '06 #7

P: n/a
> ...when I'm including dependet classess, I use require_once to avoid
multiple declarations - yet they happen...


use include_once(); rather than require_once();

ECRIA
http://www.ecria.com
Feb 24 '06 #8

P: n/a
ECRIA Public Mail Buffer wrote:
...when I'm including dependet classess, I use require_once to avoid
multiple declarations - yet they happen...

use include_once(); rather than require_once();

ECRIA
http://www.ecria.com


No, require_once will work just fine. The only difference is you get a
fatal error if require_once fails, and only a warning if include_once fails.

In this case I would suspect he wants it to fail if the file can't be
found, so require_once would be appropriate.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 24 '06 #9

P: n/a
I have come across the same problem in my own framework. The way I got
around it was to use class_exists() before the require() statement, so if
the class definition already existed I did not load it again.

--
Tony Marston

http://www.tonymarston.net

"Kimmo Laine" <sp**@outolempi.net> wrote in message
news:Hi*****************@reader1.news.jippii.net.. .
I'm flipping my wig here, people. I'm using classes and making each class
a file. when I'm including dependet classess, I use require_once to avoid
multiple declarations - yet they happen. I put debug_print_backtrace in
the file to see how it is included, and here's the output:
#0 require_once() called at [\eKirje.textGrid.class.php:4]
#1 require_once(\eKirje.textGrid.class.php) called at
[\lasku.eKirjeLasku.class.php:3]#0 require_once() called at
[\eKirje.kanava.class.php:3]
#1 require_once(\eKirje.kanava.class.php) called at
[\eKirje.EPL8.class.php:3]
#2 require_once(\eKirje.EPL8.class.php) called at
[\eKirje.kirje.class.php:3]
#3 require_once(\eKirje.kirje.class.php) called at
[\lasku.eKirjeLasku.class.php:5]
<br />
<b>Fatal error</b>: Cannot redeclare class boxcontainer in
<b>\eKirje.boxcontainer.class.php</b> on line <b>5</b><br />As you see, it
does get required twice regardless of the use of require_once in each
call. And eventually the class gets declared again. My fix for the problem
was to use

if( !in_array('boxcontainer', get_declared_classes()) ) {
require_once('eKirje.boxContainer.class.php');
}

in the files and now it works, but I'm just totally baffeld of why this is
happening? How come the require_once fails to function? Am I missing
something here?

I made the simplest test case where I had four files where in the first of
them I declare a class, then require_once it to two other files and then
finally require_once the two files to a fourth file. In this case I did
not get redeclaration errors, for some reason it worked okay then, the
class was declared only one and it worked okay.

--
"En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)

Feb 25 '06 #10

P: n/a
"Kimmo Laine" <sp**@outolempi.net> wrote in message
news:Hi*****************@reader1.news.jippii.net.. .
I'm flipping my wig here, people. I'm using classes and making each class
a file. when I'm including dependet classess, I use require_once to avoid
multiple declarations - yet they happen. I put debug_print_backtrace in
the file to see how it is included, and here's the output:
#0 require_once() called at [\eKirje.textGrid.class.php:4]
#1 require_once(\eKirje.textGrid.class.php) called at
[\lasku.eKirjeLasku.class.php:3]#0 require_once() called at
[\eKirje.kanava.class.php:3]
#1 require_once(\eKirje.kanava.class.php) called at
[\eKirje.EPL8.class.php:3]
#2 require_once(\eKirje.EPL8.class.php) called at
[\eKirje.kirje.class.php:3]
#3 require_once(\eKirje.kirje.class.php) called at
[\lasku.eKirjeLasku.class.php:5]
<br />
<b>Fatal error</b>: Cannot redeclare class boxcontainer in
<b>\eKirje.boxcontainer.class.php</b> on line <b>5</b><br />As you see, it
does get required twice regardless of the use of require_once in each
call. And eventually the class gets declared again. My fix for the problem
was to use

if( !in_array('boxcontainer', get_declared_classes()) ) {
require_once('eKirje.boxContainer.class.php');
}

in the files and now it works, but I'm just totally baffeld of why this is
happening? How come the require_once fails to function? Am I missing
something here?

I made the simplest test case where I had four files where in the first of
them I declare a class, then require_once it to two other files and then
finally require_once the two files to a fourth file. In this case I did
not get redeclaration errors, for some reason it worked okay then, the
class was declared only one and it worked okay.

Well... now I'm going out of my mind. Now it actually works. I don't know
what I've changed, but for some reason now it all works perfectly. As
someone suggested, it might have been a problem with the casing, ie.
different casing in the filename, since at some point I copied the require
statement from the other to the other, and later it started working. I gotta
try to find an earlier copy from backups to see if it was a case of cases
indeed.

By the way, someone asked about the versio, I'm running PHP 5.0.5 with IIS6
on a Windows 2003 Server.

Thank you all for replying. I'll let you know if I can backtrace the
problem, but at the moment I strongly suspect that it actually was something
like spelling it require_once('eKirje.boxContainer.class.php'); in one file
and require_once('eKirje.boxcontainer.class.php'); in the other. Blasted
case-insensitive filesystem. In a *nix server this wouldn't have happened,
since case matters in filenames.

--
"En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)
Feb 27 '06 #11

P: n/a
"Jerry Stuckle" <js*******@attglobal.net> wrote in message
news:od******************************@comcast.com. ..
ECRIA Public Mail Buffer wrote:
...when I'm including dependet classess, I use require_once to avoid
multiple declarations - yet they happen...

use include_once(); rather than require_once();

ECRIA
http://www.ecria.com


No, require_once will work just fine. The only difference is you get a
fatal error if require_once fails, and only a warning if include_once
fails.

In this case I would suspect he wants it to fail if the file can't be
found, so require_once would be appropriate.


Yes, you are correct. I *am* using require rather than include on purpouse
exactly for this reason.

--
"En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)
Feb 27 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.