如何封装一个文件包含?

发布于 2024-09-24 19:12:58 字数 148 浏览 10 评论 0原文

如果我有一个包含带有常量的文件的类,如下所示:

define("FOO", "bar");

有没有办法使该类包含带有封装的文件,因此如果我在已经定义了 FOO 常量的地方使用该类不会坏吗?

If I have a class that includes a file with a constant like so:

define("FOO", "bar");

Is there a way to make the class include the file with encapsulation so if I use the class somewhere that already has a FOO constant defined it won't break?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冬天旳寂寞 2024-10-01 19:12:58

创建一个静态类并使用常量将是封装特定常量的最佳方法:

static class Constants
{
    const Name = 'foo';
    const Path = 'Bar';
}

然后像这样使用:

echo Constants::Name; //foo
echo Constants::Path; //bar

关于预检查,您可以执行

function _defined($key,$check_classes = false)
{
    if($check_classes)
    {
        foreach(get_declared_classes() as $class)
        {
            if(constant($class . '::' . $key) !== null)
            {
                return true;
            }
        }
    }
    if(!defined($key)) //global Scope
    {
        return true;
    }
}

用法:

class a
{
    const bar = 'foo';
}

if(_defined('bar',true)) //This would be true because its within a
{
    //Blah
}

如果您想到这样的情况,

class a
{
    const b = '?';
}
class b
{
    const b = '?';
}

则常量在类范围内,因此它们不会互相影响!

Create a static class and use constants would be the best way to encapsulate specific constants:

static class Constants
{
    const Name = 'foo';
    const Path = 'Bar';
}

And then use like so:

echo Constants::Name; //foo
echo Constants::Path; //bar

in regards to the precheck you can do

function _defined($key,$check_classes = false)
{
    if($check_classes)
    {
        foreach(get_declared_classes() as $class)
        {
            if(constant($class . '::' . $key) !== null)
            {
                return true;
            }
        }
    }
    if(!defined($key)) //global Scope
    {
        return true;
    }
}

Usage:

class a
{
    const bar = 'foo';
}

if(_defined('bar',true)) //This would be true because its within a
{
    //Blah
}

If your thinking of a situation like so

class a
{
    const b = '?';
}
class b
{
    const b = '?';
}

the constants are within the class scope so they would have no affect on one another !

怎会甘心 2024-10-01 19:12:58

您可以使用 define 检查常量是否已定义:

<?php
define("FOO", "1");
if (!defined("FOO")) { ## check if constant is not defined yet
    define("FOO", "2");
}
echo FOO;
?>

You can check if constant already defined using defined:

<?php
define("FOO", "1");
if (!defined("FOO")) { ## check if constant is not defined yet
    define("FOO", "2");
}
echo FOO;
?>
可遇━不可求 2024-10-01 19:12:58

您可以使用类常量,

class Foo
{
    constant FOO = 'bar'
}

但是,您必须先包含该类,然后才能在 Foo::FOO 中使用该常量。常规常量的另一种替代方法是使用供应商前缀为它们添加前缀,以减少冲突的可能性,例如

define('JOHN_FOO', 'bar')

或使用新引入的命名空间(PHP 5.3)

define('JohnIsaacks\FOO', 'bar');

但在所有情况下,我想知道为什么你需要它。如果你想加载类,只需添加 自动加载器。

You can use a class contant

class Foo
{
    constant FOO = 'bar'
}

However, you will have to include the class before you can use the constant with Foo::FOO. An alternative with regular constants is to use to prefix them with a vendor prefix to make clashes less likely, e.g.

define('JOHN_FOO', 'bar')

or use the newly introduced namespaces (PHP 5.3)

define('JohnIsaacks\FOO', 'bar');

But in all cases, I wonder why you would need that. If you want to load classes, simply add in an autoloader.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文