This is a discussion on require_once doesn't work, "cannot redeclare class..." within the PHP Language forums, part of the PHP Programming Forums category; I'm flipping my wig here, people. I'm using classes and making each class a file. when I'm ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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ö spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg) |
|
|||
|
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 |
|
|||
|
"Erwin Moller"
<since_humans_read_this_I_am_spammed_too_much@spam yourself.com> wrote in message news:43ff0a64$0$11066$e4fe514c@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ö spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg) |
|
|||
|
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(): Quote:
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 |
|
|||
|
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. jstucklex@attglobal.net ================== |
|
|||
|
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/ |
|
|||
|
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/ |
|
|||
|
> ...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 |
|
|||
|
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. jstucklex@attglobal.net ================== |
|
|||
|
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" <spam@outolempi.net> wrote in message news:HiALf.4069$wU5.1503@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ö > spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg) > |