Search code examples
phparraysclasstypeof

Expected type 'array'. Found 'int'.intelephense(1006)


I've little problem with my PHP code. Indeed i've an error when i do array_push or implode an array: " Expected type 'array'. Found 'int'.intelephense(1006) ".

I've declare the type of $element in contructor: i don't understand.

I provide you my code.

My code in picture

(I can't paste my code: it is too long)

Thank for your help. Cyril.


Solution

  • array_push takes a reference of your array and the elements to be added.

    Your code should look like this:

    array_push(&$this->element, $element);
    

    or a more modern equivalent (and technically faster) variant:

    $this->element[] = $element;
    

    The [] operator on an array adds the element.