全局 PHP CONSTANT 在类文件中可用吗?

发布于 2024-08-18 00:35:42 字数 389 浏览 2 评论 0原文

全局 PHP CONSTANT 在类文件中可用吗?

define('SITE_PATH', 'C:/webserver/htdocs/somefolder/');

然后在我的类文件中我尝试这个

public $debug_file = SITE_PATH. 'debug/debug.sql';

但这似乎不起作用,

解析错误:解析错误,期待 ','' 或';'' 中 C:\webserver\htdocs\somefolder\includes\classes\Database.class.php 第 21 行

Is a global PHP CONSTANT available inside of a Class file?

define('SITE_PATH', 'C:/webserver/htdocs/somefolder/');

Then in my class file I try this

public $debug_file = SITE_PATH. 'debug/debug.sql';

This does not seem to work though,

Parse error: parse error, expecting
','' or';'' in
C:\webserver\htdocs\somefolder\includes\classes\Database.class.php
on line 21

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

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

发布评论

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

评论(4

仲春光 2024-08-25 00:35:42

我赞同其他人所说的。由于 $debugFile 似乎是一个可选的依赖项,我建议在创建类时初始化一个合理的默认值,然后在需要时允许通过设置器注入来更改它,例如,

define('SITE_PATH', 'C:/webserver/htdocs/somefolder/');

class Klass
{
    protected $_debugFile;
    public function __construct()
    {
        $this->_debugFile = SITE_PATH. 'debug/debug.sql' // default
    }
    public function setDebugFile($path)
    {
        $this->_debugFile = $path // custom
    }
}

请注意,注入 SITE_PATH 而不是对其进行硬编码,将是更好的做法。

I second what the others said. Since $debugFile seems an optional dependency, I'd suggest to initialize a sane default on creation of the class and then allow changing it by setter injection when needed, e.g.

define('SITE_PATH', 'C:/webserver/htdocs/somefolder/');

class Klass
{
    protected $_debugFile;
    public function __construct()
    {
        $this->_debugFile = SITE_PATH. 'debug/debug.sql' // default
    }
    public function setDebugFile($path)
    {
        $this->_debugFile = $path // custom
    }
}

Note that injecting SITE_PATH, instead of hardcoding it, would be even better practice.

千纸鹤带着心事 2024-08-25 00:35:42

类声明中不能有表达式。

我建议传递路径:

public function __construct($path)
{
    $this->debug_path = $path;
}

如果您想更改路径,这会给您带来更大的灵活性,您不必更改常量,只需更改您传入的内容即可。

或者您可以创建多个具有不同路径的对象。如果它是自动加载器类,这很有用,因为您可能希望它加载多个目录。

$autoloader = new Autoload(dirname(SYS_PATH));
$autoloader->register_loader();

class Autoload
{
    public $include_path = "";

    public function __construct($include_path="")
    {
        // Set the Include Path
        // TODO: Sanitize Include Path (Remove Trailing Slash)
        if(!empty($include_path))
        {
            $this->include_path = $include_path;
        }
        else
        {
            $this->include_path = get_include_path();
        }

        // Check the directory exists.
        if(!file_exists($this->include_path))
        {
            throw new Exception("Bad Include Path Given");
        }
    }
    // .... more stuff ....
}

You cannot have an expression in a class declaration.

I would suggest passing the path in:

public function __construct($path)
{
    $this->debug_path = $path;
}

This gives you more flexibility if you ever want to change the path, you don't have to change a constant, just what you pass in.

Or you could create multiple objects that all have different paths. This is useful if it is an autoloader class, as you might want to have it load multiple directories.

$autoloader = new Autoload(dirname(SYS_PATH));
$autoloader->register_loader();

class Autoload
{
    public $include_path = "";

    public function __construct($include_path="")
    {
        // Set the Include Path
        // TODO: Sanitize Include Path (Remove Trailing Slash)
        if(!empty($include_path))
        {
            $this->include_path = $include_path;
        }
        else
        {
            $this->include_path = get_include_path();
        }

        // Check the directory exists.
        if(!file_exists($this->include_path))
        {
            throw new Exception("Bad Include Path Given");
        }
    }
    // .... more stuff ....
}
记忆で 2024-08-25 00:35:42

您不能在字段初始值设定项中使用表达式 (.)。 请参阅 PHP 手册中的示例一

You can't use expression (.) in field initializer. See example one in PHP manual

深白境迁sunset 2024-08-25 00:35:42

是的,但是,在编译时定义的属性值不能是表达式。

请参阅 http://php.net/manual/en/language.oop5.static .php

Yes, however, property values defined at compile time cannot be expressions.

See http://php.net/manual/en/language.oop5.static.php

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