gpt4 book ai didi

php - 在 php 中处理构造函数时出错

转载 作者:可可西里 更新时间:2023-10-31 23:14:07 25 4
gpt4 key购买 nike

运行以下 PHP 代码时出现以下错误

class tableData
{
private $row1 = array("Kalle1", "address1", "postal code1", "1@email.se", "070111001", "08111001");
private $row2 = array("kalle2", "address2", "postcode2", "2@email.se", "070111002", "08111002");
private $row3 = array("kalle3", "address3", "postcode3", "3@email.se", "070111003", "08111003");
private $row4 = array("kalle4", "address4", "postcode4", "4@email.se", "070111004", "08111004");
private $rader = array(array($row1),array($row2),array($row3),array($row4));
}

错误对应变量$rader:

Parse error: syntax error, unexpected '$row1' (T_VARIABLE), expecting ')' in C:\xampp\htdocs\test_k.php on line 15

谁能帮我改正这个错误?

最佳答案

The PHP manual states :

This declaration [of class properties] 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.

private $rader = array(array($row1),array($row2),array($row3),array($row4));

所以你的 $row1 不是常量值,它需要运行时信息,即变量持有的值。

当 PHP 编译您的类时,在属性初始化时,它不知道您的 $row1 变量持有什么值。

您必须将该变量设置为空或 null(无论适合什么),或者有一个 setter 方法来设置它并首先调用它,或者如果这始终是相同的数据,则使用构造函数。

基本上,您不能根据稍后在运行时来自对象的值来设置属性。

关于php - 在 php 中处理构造函数时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49724314/

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