require_once不起作用,“不能重新声明类......” [英] require_once doesn't work, "cannot redeclare class..."

查看:90
本文介绍了require_once不起作用,“不能重新声明类......”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

人们,我正在把我的假发翻过来。我正在使用课程并使每个课程成为一个

文件。当我包括dependet classess时,我使用require_once来避免

多个声明 - 但它们发生了。我将debug_print_backtrace放在

文件中以查看它是如何包含的,这里是输出:

#0 require_once()在[\ eKirje.textGrid中调用.class.php:4]

#1 require_once(\ eKeje.textGrid.class.php)调用

[\\\ xsku.eKirjeLasku.class.php: 3]#0 require_once()调用

[\ eKirje.kanava.class.php:3]

#1 require_once(\ eKirje.kanava.class。 php)调用

[\ eKirje.EPL8.class.php:3]

#2 require_once(\ eKirje.EPL8.class.php)调用

[\ eKirje.kirje.class.php:3]

#3 require_once(\ eKirje.kirje.class.php)调用

[\\\kuku.eKirjeLasku.class.php:5]

< br />

< b>致命错误< / b>:无法重新声明

< b> \ eKirje.boxcontainer.class.php< / b>中的类盒子容器在线< b> 5< / b>< br />如您所见,无论在每次通话中使用require_once,都需要两次


最后这个类再次被声明。我对这个问题的解决方法是

使用


if(!in_array(''boxcontainer'',get_declared_classes())){

require_once(''eKirje.boxContainer.class.php'');

}

文件中的
现在它可以正常工作,但我'我完全不知道为什么这会发生什么?b $ b发生了什么?为什么require_once无法正常运行?我错过了

这里有什么东西吗?

我做了最简单的测试用例,我有四个文件在第一个

他们声明了一个类,然后require_once它到另外两个文件然后

最后require_once这两个文件到第四个文件。在这种情况下,我没有

得到重新声明错误,由于某种原因它工作正常,那个类是

只声明一个,它工作正常。
< br $> b $ b -

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

解决方案

Kimmo Laine写道:

我'人们,把我的假发翻过来。我正在使用课程并使每个班级成为一个文件。当我包括dependet classess时,我使用require_once来避免多个声明 - 但它们发生了。我将debug_print_backtrace放在文件中以查看它是如何被包含的,这里是输出:
#0 require_once()在[\ eKirje.textGrid.class.php:4]中调用<调用
[\\\ [!] eKirje.kanava.class.php:3]
#1 require_once(\ eKirje.kanava.class.php)调用
[\ eKirje.EPL8.class.php:3]
#2 require_once(\ eKirje.EPL8.class.php)调用
[\ eKirje.kirje.class.php:3]
#3 require_once(\ eKirje.kirje.class。 php)调用
[\\\kuku.eKirjeLasku.class.php:5]
< br />
< b>致命错误< / b>:无法重新声明类boxcontainer在
< b> \ eKirje.boxcontainer.class.php< / b>在线< b> 5< / b>< br />如您所见,无论在每次
通话中使用require_once,都需要两次。最终这个类再次被宣布。我对这个问题的解决方法是使用

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

在文件中,现在它可以工作,但我只是完全不知道为什么会发生这种情况?为什么require_once无法正常运行?我在这里错过了什么?

我做了最简单的测试用例,其中我有四个文件,在第一个
中我声明了一个类,然后require_once到另外两个文件然后
最后require_once将这两个文件转换为第四个文件。在这种情况下,我没有得到重新声明错误,由于某种原因,它工作正常,
类只被声明为一个,它工作正常。



你好,


要求和require_once对FILES采取行动,而不是对其内容采取行动。

如果文件包含对象定义则无关紧要。


所以假设你有:

file1.php包含对象X

file2.php还包含对象X


然后:

include_once(''file1.php);

include_once(''file2.php);


会导致一个双重的objectX声明。

你的问题可能是由这样的事情引起的吗?

你可能在不同的文件中有同一个类吗?


问候,

Erwin Moller


" Erwin Moller"

< si ****************************************** @ spam yourself.com>写在

消息新闻:43 *********************** @ news.xs4all.nl ...

Kimmo Laine写道:

我在这里翻动我的假发,人们。我正在使用课程并使每个班级成为一个文件。当我包括dependet classess时,我使用require_once来避免多个声明 - 但它们发生了。我将debug_print_backtrace放在文件中以查看它是如何被包含的,这里是输出:
#0 require_once()在[\ eKirje.textGrid.class.php:4]中调用<调用
[\\\ [!] eKirje.kanava.class.php:3]
#1 require_once(\ eKirje.kanava.class.php)调用
[\ eKirje.EPL8.class.php:3]
#2 require_once(\ eKirje.EPL8.class.php)调用
[\ eKirje.kirje.class.php:3]
#3 require_once(\ eKirje.kirje.class。 php)调用
[\\\kuku.eKirjeLasku.class.php:5]
< br />
< b>致命错误< / b>:无法重新声明类boxcontainer在
< b> \ eKirje.boxcontainer.class.php< / b>在线< b> 5< / b>< br />如您所见,
它确实需要两次,无论在每次
通话中使用require_once。最终这个类再次被宣布。我对
问题的解决方法是使用

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

现在它可以工作了,但我完全不知道为什么这个
是发生了什么?为什么require_once无法正常运行?我在这里错过了什么?

我做了最简单的测试用例,我有四个文件,在第一页
它们我宣布一个类,然后require_once它到另外两个文件然后
最后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 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)

解决方案

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


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


这篇关于require_once不起作用,“不能重新声明类......”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆