【问题标题】:Access constant in class [duplicate]类中的访问常量[重复]
【发布时间】:2013-08-23 03:55:55
【问题描述】:

我尝试访问类中的常量:

file1.php

[...]    
define('SUBDOMAIN','name');
require_once 'file2.php';
[...]

file2.php

[...]
class Gdl_Ute extends Response_Model {    
public $table = SUBDOMAIN.'zi_utp';

public function statuses()
{
[...]

我使用了一些变体,例如:

public function __construct()
{
self::$table = SUBDOMAIN.'zi_utp';
}

但是总有这样的错误:

PHP Parse error:  syntax error, unexpected '.', expecting ',' or ';'

syntax error, unexpected T_VARIABLE

非常感谢任何提示!我还阅读了有关 stackoverflow 的其他类似问题,但没有人正确回答这个问题:/

更新 现在我测试了以下解决方案:

file2.php

public $table;
public function __construct()
{
self::$table = SUBDOMAIN.'zi_utp';
}

file1.php

define('SUBDOMAIN',$subdomain);
const SUBDOMAIN = $subdomain;

结果

Access to undeclared static property OR
syntax error, unexpected T_VARIABLE

【问题讨论】:

  • 您只能将变量初始化为标量值(或数组)。如果你只是声明public $table 然后在构造函数中初始化它应该可以工作。

标签: php class


【解决方案1】:

使用const 定义您的常量。看看here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-19
    • 2010-09-12
    • 1970-01-01
    • 2017-07-06
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    • 2015-06-14
    相关资源
    最近更新 更多