如何在类中包含定义常量的文件(及其范围) [英] How to include a file that defines constants in a class (and it's scope)
问题描述
假设我们有以下内容:
some.class.php
class
{
public __construct()
{
fun_stuff();
}
}
configuration.inc
const SOMECONST = 1;
const SOMEOTHERCONST = 2;
我希望做这样的事情:
some.class.php
class
{
public __construct()
{
include_once(configuration.inc);
fun_stuff();
}
}
现在这样可行,但是常量未定义在类的范围内( echo some :: SOMECONST;
),而是在全局范围内定义( echo SOMECONST;
)
Now this works, but the constant is not defined within the scope of the class (echo some::SOMECONST;
) but rather in the global scope (echo SOMECONST;
)
我真的希望将常量放在另一个文件中,因为它在我的案例中很有意义。有没有办法在类的范围内声明常量?我知道在类定义中使用 includes
或需要
是不可能的,所以我不知所措。
I really really want to have the constants in another file as it makes a lot of sense in my case. Is there a way to declare the constants in the scope of the class? I know it's impossible to use includes
or requires
inside the class definition so i'm at a loss.
推荐答案
简单地说,没有php的扩展是不可能的。我最后只是在类文件中定义了自己的consts。
Simply put, it is not possible without extensions to php. I ended up just defining the consts in the class file its self.
这篇关于如何在类中包含定义常量的文件(及其范围)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!