Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 类中的访问常量_Php_Class - Fatal编程技术网

Php 类中的访问常量

Php 类中的访问常量,php,class,Php,Class,我尝试访问类中的常量: file1.php file2.php 我使用了一些变体,如: public function __construct() { self::$table = SUBDOMAIN.'zi_utp'; } 但总有这样的错误: PHP Parse error: syntax error, unexpected '.', expecting ',' or ';' 或 非常感谢您的任何提示!我还阅读了有关stackoverflow的其他类似问题,但没有人正确回答此问题:/ 更

我尝试访问类中的常量:

file1.php

file2.php

我使用了一些变体,如:

public function __construct()
{
self::$table = SUBDOMAIN.'zi_utp';
}
但总有这样的错误:

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

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

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

file2.php

file1.php

结果

Access to undeclared static property OR
syntax error, unexpected T_VARIABLE

使用
const
定义常数。看看

您只能将变量初始化为标量值(或数组)。如果您只是声明
public$table
,那么在构造函数中初始化它应该可以工作。
PHP Parse error:  syntax error, unexpected '.', expecting ',' or ';'
syntax error, unexpected T_VARIABLE
public $table;
public function __construct()
{
self::$table = SUBDOMAIN.'zi_utp';
}
define('SUBDOMAIN',$subdomain);
const SUBDOMAIN = $subdomain;
Access to undeclared static property OR
syntax error, unexpected T_VARIABLE