全局 PHP CONSTANT 在类文件中可用吗?
全局 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我赞同其他人所说的。由于 $debugFile 似乎是一个可选的依赖项,我建议在创建类时初始化一个合理的默认值,然后在需要时允许通过设置器注入来更改它,例如,
请注意,注入 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.
Note that injecting SITE_PATH, instead of hardcoding it, would be even better practice.
类声明中不能有表达式。
我建议传递路径:
如果您想更改路径,这会给您带来更大的灵活性,您不必更改常量,只需更改您传入的内容即可。
或者您可以创建多个具有不同路径的对象。如果它是自动加载器类,这很有用,因为您可能希望它加载多个目录。
You cannot have an expression in a class declaration.
I would suggest passing the path in:
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.
您不能在字段初始值设定项中使用表达式 (.)。 请参阅 PHP 手册中的示例一
You can't use expression (.) in field initializer. See example one in PHP manual
是的,但是,在编译时定义的属性值不能是表达式。
请参阅 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