gpt4 book ai didi

php - 如何在 PHP 中的对象变量中执行文本连接?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:01:50 26 4
gpt4 key购买 nike

如果我在 PHP 类中使用“点”为变量赋值,则会失败。

例如:

class bla {
public $a = 'a' . 'b';
}

否则我应该如何处理这个问题?

最佳答案

您只能在构造函数中执行此操作,因为必须在使用常量表达式声明时初始化类变量/属性。来自manual :

This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

这意味着您不能使用任何运算符或函数调用。

class bla {
public $a;

public function __construct() {
$this->a = 'a' . 'b';
}
}

关于php - 如何在 PHP 中的对象变量中执行文本连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5051553/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com