PHP8.1只读属性是否与BIND_PARAM兼容?
下面是我的构造函数
public function __construct(
private readonly ?bool $alreadyLive, //If true, the article is already live and will be updated. If false, it is a new article
private readonly bool $pushLive, //If true, the article will be pushed live. If false, it will be saved in pending_articles
private string $articleTitle,
private string $articleExcerpt,
private string $articleContent,
private string $imageCard,
private readonly string $articleType,
private readonly string $articleStatus,
private readonly ?int $articleNumber,
private readonly int $authorID,
private readonly int $heroPosition,
databaseconnection $db
) {
$this->db = $db;
}
我正在try 将一些数据插入到数据库中,如下所示:
try {
$sql = "INSERT INTO " . $table . "
(title, author, content, excerpt, image_card,
views, hero_position, type, status)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = $connection->prepare($sql);
$stmt->bind_param("sisssiiss",
$this->articleTitle, $this->authorID,
$this->articleContent, $this->articleExcerpt,
$this->imageCard, $views, $this->heroPosition,
$this->articleType, $this->articleStatus
);
$stmt->execute();
$article_id = $stmt->insert_id;
$stmt->close();
} catch (\Exception) {
//Rollback the transaction
$connection->rollback();
//Return error message
$response['status'] = 'error';
$response['message'] = 'There was an error submitting your article. Please try again.';
return $response;
}
但我一直收到这样的错误:
Cannot modify readonly property article::$authorID
根据堆栈跟踪中的行,它是由此行引起的:
$stmt->bind_param("sisssiiss", $this->articleTitle, $this->authorID, $this->articleContent, $this->articleExcerpt, $this->imageCard, $views, $this->heroPosition, $this->articleType, $this->articleStatus);