Bug 373414

Summary: Accessing $this as an array marks the code as syntax error, even though it is a correct syntax in PHP
Product: [Applications] kdevelop Reporter: Jan Pavlicek <jan.pavlicek>
Component: Language Support: PHPAssignee: kdevelop-bugs-null
Status: RESOLVED DUPLICATE    
Severity: normal CC: alexander
Priority: NOR    
Version: 5.0.3   
Target Milestone: ---   
Platform: Arch Linux   
OS: Linux   
Latest Commit: Version Fixed In:

Description Jan Pavlicek 2016-12-08 08:15:10 UTC
Using this construction, which is perfectly valid in PHP:

<?php

class A implements \ArrayAccess
{

    public $values = [];


    public function setTest()
    {
        $this['test'] = 'test';
    }



    public function offsetGet($offset)
    {
        return $this->values[$offset];
    }



    public function offsetSet($offset, $value)
    {
        $this->values[$offset] = $value;
    }



    public function offsetExists($offset)
    {
        return array_key_exists($offset, $this->values);
    }



    public function offsetUnset($offset)
    {
        unset($this->values[$offset]);
    }
}


$test = new A;
$test->setTest();
echo $test['test'];


there will be a red background for the line $this['test'] = 'test', saying "Cannot re-assign $this". But that is in fact possible due to $this being an instance of class implementing the ArrayAccess interface, thus allowing it to be accessed as array.

Please look in to this, many frameworks use this kind of access heavily for forms, components etc. and it is very distracting.

Thanks and have a nice day!
Comment 1 Alexander Zhigalin 2017-02-28 08:45:02 UTC

*** This bug has been marked as a duplicate of bug 346876 ***