问题
I have to convert Twig template back into PHP for a site so we are basically mimicking the functionality. I'm getting an error from PHP highlighting my class property $link which contains an array of other variables.
Constant expression contains invalid operations
I don't understand why it is highlighting this property but not my other property directly underneath named $downloadLink. Both are arrays, unless I'm missing something. This error did not highlight initially but only later as I continued the rest of my code.
class Card {
public $title;
public $image;
public $text;
public $link = array(
$url,
$nale,
);
public $downloadLink = array(
$url,
$title,
$type,
$weight,
);
function __construct(string $title, string $image, string $text, array $link_arr, array $downloadLink_arr)
{
$this->title = $title;
$this->image = $image;
$this->text = $text;
$this->link->url = $link_arr[0];
$this->link->nale = $link_arr[1];
$this->downloadLink->url = $downloadLink_arr[0];
$this->downloadLink->title = $downloadLink[1];
$this->downloadLink->type = $downloadLink[2];
$this->downloadLink->weight = $downloadLink[3];
}
}
回答1:
This is a PHP basic stuff.
You can't initialize variables with variables and you are trying to use Object assignments when your variable is an array.
<?php
class Card
{
public $title;
public $image;
public $text;
public $link = array();
public $downloadLink = array();
public function __construct(string $title, string $image, string $text, array $link_arr, array $downloadLink_arr)
{
$this->title = $title;
$this->image = $image;
$this->text = $text;
$this->link['url'] = $link_arr[0];
$this->link['nale'] = $link_arr[1];
$this->downloadLink['url'] = $downloadLink_arr[0];
$this->downloadLink['title'] = $downloadLink[1];
$this->downloadLink['type'] = $downloadLink[2];
$this->downloadLink['weight'] = $downloadLink[3];
}
}
来源:https://stackoverflow.com/questions/54368621/constant-expression-contains-invalid-operations-not-highlighting-other-variabl