发生了什么?为什么require_once无法正常运行?我在这里错过了什么? require和require_once对FILES采取行动,而不是对其内容采取行动。如果文件包含对象定义则无关紧要。 是的,这正是我理解的,这里没有问题。 所以假设你有:包含对象X的file1.php file2.php另外包含对象X 然后: include_once(''file1.php); include_once(''file2.php); 否,见下文。 将导致双重objectX声明。 您的问题可能是由此类问题引起的?你可能在不同的文件中有相同的课程吗? 不,绝对没有。我有一个文件A.php包含A类和文件F.php 和G.php都有require_once(" A.php");然后我有一个页面D.php 有require_once(" G.php");和require_once(" F.php");得到了 多重声明,因为它们最终都需要A.php,因为我已经使用了require_once,所以不应该是 ...... - En ole paha ihminen,mutta omenat ovat elinkeinoni。 -Perttu Sirvi? sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv *** @ bhgbyrzcv.arg) Kimmo Laine写道: < snip> 不,绝对没有。我有一个文件A.php包含A类和文件 F.php和G.php都有require_once(" A.php");然后我有一个页面 D.php,它有require_once(" G.php");和require_once(" F.php"); 产生了多重声明,因为它们最终都需要 A.php,因此我不应该使用require_once ... aha。那就是问题所在,但它很微妙。 这就是PHP.net所说的require_once(): require_once()语句在执行脚本期间包含并评估指定的文件 。这是一种类似于 require()语句的行为,唯一的区别是如果已经包含来自 文件的代码,则不会包含再次。有关此声明 如何工作的更多信息,请参阅 文档中的require()。 所以你可能期望PHP计算一个特定文件的数量,需要,但这不是它所说的:它只寻找相同的文件。 差异可能看起来不大,但这正是导致你的问题的原因:你的包含用于不同的文件,因此包括每个 时间,因此多次声明。 问候, Erwin Moller I''m flipping my wig here, people. I''m using classes and making each class afile. when I''m including dependet classess, I use require_once to avoidmultiple declarations - yet they happen. I put debug_print_backtrace in thefile 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, itdoes 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 touseif( !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 ishappening? How come the require_once fails to function? Am I missingsomething here?I made the simplest test case where I had four files where in the first ofthem I declare a class, then require_once it to two other files and thenfinally require_once the two files to a fourth file. In this case I did notget redeclaration errors, for some reason it worked okay then, the class wasdeclared 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) 解决方案 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 Xfile2.php ALSO containing object XThen: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"<si******************************************@spam yourself.com> wrote inmessage 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.phpand G.php which both have require_once("A.php"); Then I have a page D.phpwhich has require_once("G.php"); and require_once("F.php"); resulting themultiple declaration, since they both eventually require A.php, which theyshouldn''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)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 fileduring the execution of the script. This is a behavior similar to therequire() statement, with the only difference being that if the code from afile has already been included, it will not be included again. See thedocumentation for require() for more information on how this statementworks.So you might expect that PHP is counting the number os time a certain fileis 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 yourproblem: Your include is used in different files, and thus included everytime, hence the multiple declaration.Regards,Erwin Moller 这篇关于require_once不起作用,“不能重新声明类......”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 18:48