You are here: Re: require_once doesn't work, "cannot redeclare class..." « PHP Programming Language « IT news, forums, messages
Re: require_once doesn't work, "cannot redeclare class..."

Posted by Kimmo Laine on 02/24/06 15:42

"Erwin Moller"
<since_humans_read_this_I_am_spammed_too_much@spamyourself.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)

 

Navigation:

[Reply to this message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация