Constant expression contains invalid operations - Not highlighting other variable?

久未见 提交于 2021-01-29 05:38:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!