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

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; "Kimmo Laine" <spam@outolempi.net> wrote in message news:HiALf.4069$wU5.1503@reader1.news.jippii....


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #11 (permalink)  
Old 02-27-2006
Kimmo Laine
 
Posts: n/a
Default Re: require_once doesn't work, "cannot redeclare class..."

"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.



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ö
spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg)


Reply With Quote
  #12 (permalink)  
Old 02-27-2006
Kimmo Laine
 
Posts: n/a
Default Re: require_once doesn't work, "cannot redeclare class..."

"Jerry Stuckle" <jstucklex@attglobal.net> wrote in message
news:odKdnQnIM4Iv4WLenZ2dnUVZ_s2dnZ2d@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ö
spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg)


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 12:16 PM.


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