bmewburn / vscode-intelephense

PHP intellisense for Visual Studio Code

Home Page:https://intelephense.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Double $ is not marked as error

zeroSal opened this issue · comments

Describe the bug
Intelephese does not recognize as error a variable with double $ even if it is a function parameter.

To Reproduce

class MyTest
{
    /** @var string */
    private $myString;

    public function __construct(string $myString)
    {
        $this->myString = $$myString;
    }

    public function myFunction(string $parameter): void
    {

        var_dump($$parameter . $$myString);
    }
}

Expected behavior
Intelephese marks as error the line where the variable is references with double $

Screenshots
image

Platform and version

  • Linux (Ubuntu 22.04)
  • Intelephese Premium (version 1.10.4)
  • PHP 7.4
  • Visual Studio Code (version 1.88.1)

Hi, are you sure that double $ is an error? The problem in your code is the scope of the variables.

Hi, this is not error at ALL!
It's a language syntax, read PHP doc: PHP.net: Variable variables

According to PHP.net:

Sometimes it is convenient to be able to have variable variable names. That is, a variable name which can be set and used dynamically. A normal variable is set with a statement such as:

<?php
$a = 'hello';
?>

A variable variable takes the value of a variable and treats that as the name of a variable. In the above example, hello, can be used as the name of a variable by using two dollar signs. i.e.

<?php
$$a = 'world';
?>

At this point two variables have been defined and stored in the PHP symbol tree: $a with contents "hello" and $hello with contents "world". Therefore, this statement:

<?php
echo "$a {$$a}";
?>

produces the exact same output as:

<?php
echo "$a $hello";
?>

This is not an error. Thanks @hosni and @Xring-git for explaining me.