简体   繁体   English

是否在Class文件中提供了全局PHP CONSTANT?

[英]Is a global PHP CONSTANT available inside of a Class file?

Is a global PHP CONSTANT available inside of a Class file? 是否在Class文件中提供了全局PHP CONSTANT?

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 解析错误:在第21行的C:\\ webserver \\ htdocs \\ somefolder \\ includes \\ classes \\ Database.class.php中解析错误,期待','' or ';''

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, eg 由于$ debugFile似乎是一个可选的依赖项,我建议在创建类时初始化一个合理的默认值,然后允许在需要时通过setter注入来更改它,例如

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. 请注意,注入SITE_PATH而不是硬编码将是更好的做法。

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 ....
}

You can't use expression (.) in field initializer. 您不能在字段初始值设定项中使用表达式(。)。 See example one in PHP manual 请参阅PHP手册中的示例一

Yes, however, property values defined at compile time cannot be expressions. 是的,但是,在编译时定义的属性值不能是表达式。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM